一.十六进制SCII转十进制字符串
例如:十六进制ASCII :BEA94A4C313233343536
转成 字符串 :京JL123456
1.封装方法
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
public class HexAsciiConverter {
/**
* 十六进制ASCII字符串转可读字符串
* @param hexStr 十六进制字符串(如 BEA94A4C313233343536)
* @param charset 字符编码(如 StandardCharsets.UTF_8)
* @return 解码后的字符串(如 "京JL123456")
*/
public static String hexAsciiToString(String hexStr, Charset charset) {
if (hexStr == null || hexStr.length() % 2 != 0) {
throw new IllegalArgumentException("十六进制字符串长度必须为偶数");
}
byte[] bytes = new byte[hexStr.length() / 2];
for (int i = 0; i < bytes.length; i++) {
int high = Character.digit(hexStr.charAt(i * 2), 16) << 4;
int low = Character.digit(hexStr.charAt(i * 2 + 1), 16);
bytes[i] = (byte) (high | low); // 合并高低4位[2,6](@ref)
}
return new String(bytes, charset); // 按指定编码解码
}
/**
* 字符串转十六进制ASCII(反向操作)
* @param text 原始字符串(如 "京JL123456")
* @param charset 字符编码
* @return 十六进制字符串(如 BEA94A4C313233343536)
*/
public static String stringToHexAscii(String text, Charset charset) {
byte[] bytes = text.getBytes(charset);
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
hex.append(String.format("%02X", b)); // 双字符补齐[7](@ref)
}
return hex.toString();
}
}
2.调用示例
public class Main {
public static void main(String[] args) {
String hexStr = "BEA94A4C313233343536";
// 使用 GBK 编码解析(兼容中文)
String result = HexAsciiConverter.hexAsciiToString(hexStr, Charset.forName("GBK"));
System.out.println(result); // 输出:京JL123456
}
}
3. 反向转换验证
String originalText = "京JL123456";
String hexResult = HexAsciiConverter.stringToHexAscii(originalText, Charset.forName("GBK"));
System.out.println(hexResult); // 输出:BEA94A4C313233343536
二.两字节十六进制转十进制
例如:038e 转换成 1000
public class HexToDecimal {
public static void main(String[] args) {
String hexStr = "038e";
try {
int decimalValue = Integer.parseInt(hexStr, 16);
System.out.println("十六进制 " + hexStr + " → 十进制: " + decimalValue);
} catch (NumberFormatException e) {
System.out.println("输入格式错误:" + hexStr);
}
}
}
三.十六进制字符串转Byte数组
1.封装方法
public static byte[] hexStringToByteArray(String hex) {
if (hex == null || hex.length() % 2 != 0) {
throw new IllegalArgumentException("十六进制字符串长度必须为偶数");
}
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) {
int index = i * 2;
String byteStr = hex.substring(index, index + 2);
bytes[i] = (byte) Integer.parseInt(byteStr, 16); // 按16进制解析
}
return bytes;
}
2.调用示例
String hex = "1A2B3C4D"; // 十六进制字符串
byte[] byteArray = hexStringToByteArray(hex);
System.out.println(Arrays.toString(byteArray));
// 输出: [26, 43, 60, 77](十进制)或 [0x1A, 0x2B, 0x3C, 0x4D](十六进制)
四.Byte数组转十六进制字符串
1.封装方法
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
int unsignedByte = b & 0xFF; // 关键:避免负数符号扩展[4,6](@ref)
String hex = Integer.toHexString(unsignedByte);
if (hex.length() == 1) {
sb.append('0'); // 补零确保双字符格式[1,2](@ref)
}
sb.append(hex);
}
return sb.toString();
}
2.调用示例
byte[] data = {10, -86, 74}; // 示例字节数组
System.out.println(bytesToHex(data)); // 输出:"0aaa4a"
五.硬件调试
遇到通过wifi接受的数据,要通过转换成十六进制字符串,或者最后又是十进制数据。都是根据双方的协议来开发的。那么我发送过去的数据也需要,经过特殊转换成byte字节发过去,硬件那边收到不至于乱码的数据。
1、硬件调试发给android这边是十六进制数据
原始数据:
68 38 38 68 A 72 78 55 34 12 43 23 01 07 Y 00 00 00 0C 13 78 56 34 12 0C 3B 78 34 12 0C 26 78 56 34 12 0B 59 45 23 00 02 FD 17 00 CS 16
android读到数据是byte字节数组
DataInputStream dis = new DataInputStream(new
BufferedInputStream(socket.getInputStream()));
byte readBuffer[] = new byte[64];
int count = 0;
try {
count = dis.read(readBuffer);
} catch (IOException e) {
continue;
}
readBuffer收到数据是这样的:
104, 56, 56, 104, 0, 114, 120, 85, 52, 18, 67, 35, 1, 7, 0, 0, 0, 0, 12, 19, 120, 86, 52, 18, 12, 59, 120, 52, 18, 12, 38, 120, 86, 52, 18, 11, 89, 69, 35, 0, 2, -3, 23, 0, 0, 22
那么要根据这些数据转换成十六进制的字符串,如果你直接转换成String字符串那肯定乱码了。因为硬件调试发给android这边是十六进制数据。
readBuffer字节数组转后数据十六进制是这样的:
68 , 38 , 38 , 68 , 00 , 72 , 78 , 55 , 34 , 12 , 43 , 23 , 01 , 07 , 00 , 00 , 00 , 00 , 0C , 13 , 78 , 56 , 34 , 12 , 0C , 3B , 78 , 34 , 12 , 0C , 26 , 78 , 56 , 34 , 12 , 0B , 59 , 45 , 23 , 00 , 02 , FD , 17 , 00 , 00 , 16
可以看出跟硬件发的是不是一样了。这里面不是十六进制Y,CS就用00填充了。
当然这些都是根据双方收发数据来解析,处理的。
2、android端发给硬件那边
android的原始数据是这个的:
68 04 04 68 35 FD 50 00 A0 16,
也是十六进制的字符串
需要做的就是十六进制的字符串转换成byte数组
private String mstrRestartSend = "FE FE 68 04 04 68 53 FD 50 00 A0 16";
private byte[] mRestart = null;
mRestart = StringUtil.HexCommandtoByte(mstrRestartSend.getBytes());
public class StringUtil {
// 十六进制的字符串转换成byte数组
public static byte[] HexCommandtoByte(byte[] data) {
if (data == null) {
return null;
}
int nLength = data.length;
String strTemString = new String(data, 0, nLength);
String[] strings = strTemString.split(" ");
nLength = strings.length;
data = new byte[nLength];
for (int i = 0; i < nLength; i++) {
if (strings[i].length() != 2) {
data[i] = 00;
continue;
}
try {
data[i] = (byte)Integer.parseInt(strings[i], 16);
} catch (Exception e) {
data[i] = 00;
continue;
}
}
return data;
}
}
那么这样发过去就不会错误或者乱码。
很多初学者,特别是在物联网方面弄不清楚这个基本数据交流的转换。
4585

被折叠的 条评论
为什么被折叠?



