开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

噬神苍龙/java进制转换工具

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
Main.java 12.29 KB
一键复制 编辑 原始数据 按行查看 历史
噬神苍龙 提交于 2022年07月22日 20:17 +08:00 . 首次提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
import java.io.*;
import java.util.Scanner;
public class Main {
private static boolean isConversion;
public static void main(String[] args) {
System.err.println("欢迎使用由JackiePenghe开发的进制转换工具!");
Scanner scanner = new Scanner(System.in);
String hexString = String.format("%08X", Long.parseLong("1"));
System.out.println("请选择转换方法:");
System.out.println("1. 10进制数据源");
System.out.println("2. 16进制数据源");
System.out.print("请选择转换方法(输入quit或exit退出程序):");
while (true) {
boolean nextLine = scanner.hasNextLine();
if (!nextLine) {
continue;
}
String s = scanner.nextLine();
if (s.equalsIgnoreCase("exit") || s.equalsIgnoreCase("quit")) {
break;
}
switch (s) {
case "1":
decimalToHex(scanner);
break;
case "2":
hexToDecimal(scanner);
break;
default:
System.out.println("输入错误,请重新输入!");
System.out.println("1. 10进制转换为16进制");
System.out.println("2. 16进制转换为10进制");
System.out.print("请选择转换方法(输入quit或exit退出程序):");
break;
}
}
}
/**
* 16进制转换为10进制
*
* @param scanner 接收输入
*/
private static void hexToDecimal(Scanner scanner) {
System.out.println("请输入文本文件路径:(路径为全路径,txt文件,回车区分每个十六进制,quit或exit退出程序)");
while (true) {
boolean nextLine = scanner.hasNextLine();
if (!nextLine) {
continue;
}
String s = scanner.nextLine();
if (s.equalsIgnoreCase("exit") || s.equalsIgnoreCase("quit")) {
break;
}
if (isConversion) {
System.out.println("正在处理上一个文件,请稍等");
continue;
}
isConversion = true;
System.out.println("正在转换,请稍后");
startHexToDecimalConvertThread(s);
}
}
// E:\Users\pengh\Desktop\新建 文本文档.txt
private static void startHexToDecimalConvertThread(String filePath) {
new Thread(() -> {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(filePath);
bufferedReader = new BufferedReader(fileReader);
while (true) {
String line = bufferedReader.readLine();
if (line == null) {
break;
}
convertHexDataAndWriteToFile(line, filePath);
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (FileWriteException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println("数据转换异常,请检查文件内容是否合法");
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("关闭文件读取流失败");
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
System.out.println("关闭文件读取流失败");
}
}
isConversion = false;
System.out.println("转换处理结束,可继续输入文件路径转换,输入exit或quit退出程序");
}
}).start();
}
private static void convertHexDataAndWriteToFile(String line, String sourceFilePath) throws FileWriteException {
String hexSource = line.trim();
if (hexSource.length() > 8) {
throw new FileWriteException("十六进制字符串长度不能超过8个字符");
}
long sourceDecimal;
try {
sourceDecimal = Long.parseLong(hexSource, 16);
} catch (NumberFormatException e) {
throw new FileWriteException("十六进制字符串格式错误,转换为十进制失败");
}
String hexRevert = revertData(hexSource);
long revertDecimal;
try {
revertDecimal = Long.parseLong(hexRevert, 16);
} catch (NumberFormatException e) {
throw new FileWriteException("翻转后的十六进制字符串格式错误,转换为十进制失败 hexRevert = " + hexRevert);
}
StringBuilder sourceDecimalStr = new StringBuilder(String.valueOf(sourceDecimal));
if (sourceDecimalStr.length() < 10) {
for (int i = 0; i < 10 - sourceDecimalStr.length(); i++) {
sourceDecimalStr.insert(0, "0");
}
}
StringBuilder revertDecimalStr = new StringBuilder(String.valueOf(revertDecimal));
if (revertDecimalStr.length() < 10) {
for (int i = 0; i < 10 - revertDecimalStr.length(); i++) {
revertDecimalStr.insert(0, "0");
}
}
File file = new File(sourceFilePath);
String name = file.getName();
String[] split = name.split("\\.");
File parentFile = file.getParentFile();
File newFile = new File(parentFile, split[0] + "-revert.txt");
try {
FileWriter fileWriter = new FileWriter(newFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter
.append("源十六进制:")
.append(hexSource)
.append("\t")
.append("源10进制:")
.append(sourceDecimalStr.toString())
.append("\t")
.append("逆序十六进制:")
.append(hexRevert)
.append("\t")
.append("逆序10进制:")
.append(revertDecimalStr.toString())
.append("\n")
.flush();
fileWriter.close();
bufferedWriter.close();
} catch (Exception e) {
throw new FileWriteException("写入转换后的数据失败");
}
}
/**
* 10进制转换为16进制
*
* @param scanner 接收输入
*/
private static void decimalToHex(Scanner scanner) {
System.out.println("请输入文本文件路径:(路径为全路径,txt文件,回车区分每个十进制,quit或exit退出程序)");
while (true) {
boolean nextLine = scanner.hasNextLine();
if (!nextLine) {
continue;
}
String s = scanner.nextLine();
if (s.equalsIgnoreCase("exit") || s.equalsIgnoreCase("quit")) {
break;
}
if (isConversion) {
System.out.println("正在处理上一个文件,请稍等");
continue;
}
isConversion = true;
System.out.println("正在转换,请稍后");
startDecimalToHexConvertThread(s);
}
}
private static void startDecimalToHexConvertThread(String filePath) {
new Thread(() -> {
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader(filePath);
bufferedReader = new BufferedReader(fileReader);
while (true) {
String line = bufferedReader.readLine();
if (line == null) {
break;
}
convertDecimalDataAndWriteToFile(line, filePath);
}
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (FileWriteException e) {
System.out.println("写入转换后的数据失败");
} catch (IOException e) {
System.out.println("数据转换异常,请检查文件内容是否合法");
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
System.out.println("关闭文件读取流失败");
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
System.out.println("关闭文件读取流失败");
}
}
isConversion = false;
System.out.println("转换处理结束,可继续输入文件路径转换,输入exit或quit退出程序");
}
}).start();
}
/**
* 翻转数据并写入到新的文件
*
* @param line 内容
* @param sourceFilePath 源文件路径
*/
private static void convertDecimalDataAndWriteToFile(String line, String sourceFilePath) throws FileWriteException {
String trim = line.trim();
String hexString = String.format("%08X", Long.parseLong(trim));
String sourceHex = hexString.toUpperCase();
String result;
try {
result = revertData(sourceHex);
} catch (NumberFormatException e) {
throw new FileWriteException("十六进制字符串翻转失败");
}
File file = new File(sourceFilePath);
String name = file.getName();
String[] split = name.split("\\.");
File parentFile = file.getParentFile();
File newFile = new File(parentFile, split[0] + "-revert.txt");
try {
FileWriter fileWriter = new FileWriter(newFile, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter
.append("正序:")
.append(sourceHex)
.append("\t")
.append("逆序:")
.append(result)
.append("\n")
.flush();
fileWriter.close();
bufferedWriter.close();
} catch (Exception e) {
throw new FileWriteException("写入转换后的数据失败");
}
}
private static String revertData(String trim) throws NumberFormatException {
byte[] bytes = hexStrToByteArray(trim);
byte[] result = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
result[bytes.length - i - 1] = bytes[i];
}
String byteArrayString = byteArrayToHexStr(result);
return byteArrayString.replace(" ", "");
}
/**
* byteArray字符串转换为Byte值
*
* @param src Byte字符串,每个Byte之间没有分隔符
* @return byte[]
*/
public static byte[] hexStrToByteArray(String src) throws NumberFormatException {
int m, n;
int l = src.length() / 2;
System.out.println(l);
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
m = i * 2 + 1;
n = m + 1;
int integer = Integer.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
ret[i] = (byte) integer;
}
return ret;
}
/**
* byteArray转换成十六进制字符串
*
* @param byteArray byte数组
* @return String 每个Byte值之间空格分隔
*/
public static String byteArrayToHexStr(byte[] byteArray) {
String stmp;
StringBuilder sb = new StringBuilder();
for (byte aByte : byteArray) {
stmp = Integer.toHexString(aByte & 0xFF);
sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
sb.append(" ");
}
return sb.toString().toUpperCase().trim();
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

java进制转换工具
暂无标签
Apache-2.0
使用 Apache-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sscl/java-binary-conversion-tool.git
git@gitee.com:sscl/java-binary-conversion-tool.git
sscl
java-binary-conversion-tool
java进制转换工具
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /