02 5f 00 01 00 08 00 b8 ca 41 38 39 31 38 36 02 00 04 00 e4 32 2e 06 03 00 04 00 6a f4 26 02 04 00 07 00 e2 07 02 0c 0c 21 15 05 00 04 00 77 54 01 00 06 00 02 00 14 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 33 36 37 39 34 36 33 33 39 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 30 30 35 34 54 02 00 04 00 85 3c 30 06 03 00 04 00 a7 6f 26 02 04 00 07 00 e2 07 02 0c 0c 21 0f 05 00 04 00 ce 0f 03 00 06 00 02 00 26 00 07 00 02 00 01 00 08 00 01 00 01 10 00 0b 00 31 33 31 30 39 34 33 35 31 34 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 30 37 34 30 02 00 04 00 aa 44 2e 06 03 00 04 00 c4 ef 26 02 04 00 07 00 e2 07 02 0c 0c 21 17 05 00 04 00 4a 58 01 00 06 00 02 00 00 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 35 32 31 34 30 37 30 32 39 32 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 41 34 37 34 02 00 04 00 7d 99 30 06 03 00 04 00 3f e2 25 02 04 00 07 00 e2 07 02 0c 0c 21 12 05 00 04 00 1e 11 03 00 06 00 02 00 06 00 07 00 02 00 03 00 08 00 01 00 01 10 00 0b 00 31 33 31 33 39 34 30 36 37 35 34 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00
import java.io.ByteArrayOutputStream;
import java.net.SocketException;
public class Test {
private static final String hexString = "0123456789ABCDEF";
public static void main(String[] args) throws SocketException {
String s = "02 5f 00 01 00 08 00 b8 ca 41 38 39 31 38 36 02 00 04 00 e4 32 2e 06 03 00 04 00 6a f4 26 02 04 00 07 00 e2 07 02 0c 0c 21 15 05 00 04 00 77 54 01 00 06 00 02 00 14 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 33 36 37 39 34 36 33 33 39 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 30 30 35 34 54 02 00 04 00 85 3c 30 06 03 00 04 00 a7 6f 26 02 04 00 07 00 e2 07 02 0c 0c 21 0f 05 00 04 00 ce 0f 03 00 06 00 02 00 26 00 07 00 02 00 01 00 08 00 01 00 01 10 00 0b 00 31 33 31 30 39 34 33 35 31 34 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 30 37 34 30 02 00 04 00 aa 44 2e 06 03 00 04 00 c4 ef 26 02 04 00 07 00 e2 07 02 0c 0c 21 17 05 00 04 00 4a 58 01 00 06 00 02 00 00 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 35 32 31 34 30 37 30 32 39 32 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 41 34 37 34 02 00 04 00 7d 99 30 06 03 00 04 00 3f e2 25 02 04 00 07 00 e2 07 02 0c 0c 21 12 05 00 04 00 1e 11 03 00 06 00 02 00 06 00 07 00 02 00 03 00 08 00 01 00 01 10 00 0b 00 31 33 31 33 39 34 30 36 37 35 34 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00";
//System.out.println(s.replaceAll("\\s+",""));
System.out.println(decode(s));
String s1 = "这是个sb";
System.out.println(encode(s1));
System.out.println(decode(encode(s1)));
}
/**
* 解码,16进制转字符串
* @param bytes
* @return
*/
public static String decode(String bytes) {
bytes=bytes.replaceAll("\\s+","");
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
/**
* 编码字符串转16进制
* @param str
* @return
*/
public static String encode(String str) {
// 根据默认编码获取字节数组
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将字节数组中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
}
}
这是十六进制数,想转成十进制可以用下面的这个方法,因为我工作中也有这样的数,跟硬件交互的时候都是按照这样的十六进制数进行交互的;
你的这个十六进制数中应该有整形(java里面叫int)和字符型(java里面叫字符串)混合数据,得看协议是什么才能准确的解析,如果是字符串十六进制数,那么可以使用下面的这个方法解析,如果是整形的数据,直接Integer.parseInt("传入的十六进制数",16) 这个方法解析就行了
/**
* 十六进制数转中文
*
* @param hexString
* @return
*/
public static String hexString2cn(String hexString) {
String cn = null;
try {
if (hexString != null && hexString.length() != 0) {
hexString = hexString.replaceAll(" ", "");
int length = hexString.length() / 2;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
bytes[i] = (byte) Integer.parseInt(
hexString.substring(i * 2, i * 2 + 2), 16);
}
cn = new String(bytes, "GBK").trim();
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return cn;
}
纯手打希望能帮到您!!
import java.io.ByteArrayOutputStream;
import java.net.SocketException;
public class Test {
private static final String hexString = "0123456789ABCDEF";
public static void main(String[] args) throws SocketException {
String s = "02 5f 00 01 00 08 00 b8 ca 41 38 39 31 38 36 02 00 04 00 e4 32 2e 06 03 00 04 00 6a f4 26 02 04 00 07 00 e2 07 02 0c 0c 21 15 05 00 04 00 77 54 01 00 06 00 02 00 14 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 33 36 37 39 34 36 33 33 39 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 30 30 35 34 54 02 00 04 00 85 3c 30 06 03 00 04 00 a7 6f 26 02 04 00 07 00 e2 07 02 0c 0c 21 0f 05 00 04 00 ce 0f 03 00 06 00 02 00 26 00 07 00 02 00 01 00 08 00 01 00 01 10 00 0b 00 31 33 31 30 39 34 33 35 31 34 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 30 37 34 30 02 00 04 00 aa 44 2e 06 03 00 04 00 c4 ef 26 02 04 00 07 00 e2 07 02 0c 0c 21 17 05 00 04 00 4a 58 01 00 06 00 02 00 00 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 35 32 31 34 30 37 30 32 39 32 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 41 34 37 34 02 00 04 00 7d 99 30 06 03 00 04 00 3f e2 25 02 04 00 07 00 e2 07 02 0c 0c 21 12 05 00 04 00 1e 11 03 00 06 00 02 00 06 00 07 00 02 00 03 00 08 00 01 00 01 10 00 0b 00 31 33 31 33 39 34 30 36 37 35 34 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00";
//System.out.println(s.replaceAll("\\s+",""));
System.out.println(decode(s));
String s1 = "这是个sb";
System.out.println(encode(s1));
System.out.println(decode(encode(s1)));
}
/**
* 解码,16进制转字符串
* @param bytes
* @return
*/
public static String decode(String bytes) {
bytes=bytes.replaceAll("\\s+","");
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
/**
* 编码字符串转16进制
* @param str
* @return
*/
public static String encode(String str) {
// 根据默认编码获取字节数组
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将字节数组中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
}
}
import java.io.ByteArrayOutputStream;
import java.net.SocketException;
public class Test {
private static final String hexString = "0123456789ABCDEF";
public static void main(String[] args) throws SocketException {
String s = "02 5f 00 01 00 08 00 b8 ca 41 38 39 31 38 36 02 00 04 00 e4 32 2e 06 03 00 04 00 6a f4 26 02 04 00 07 00 e2 07 02 0c 0c 21 15 05 00 04 00 77 54 01 00 06 00 02 00 14 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 33 36 37 39 34 36 33 33 39 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 30 30 35 34 54 02 00 04 00 85 3c 30 06 03 00 04 00 a7 6f 26 02 04 00 07 00 e2 07 02 0c 0c 21 0f 05 00 04 00 ce 0f 03 00 06 00 02 00 26 00 07 00 02 00 01 00 08 00 01 00 01 10 00 0b 00 31 33 31 30 39 34 33 35 31 34 37 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 30 37 34 30 02 00 04 00 aa 44 2e 06 03 00 04 00 c4 ef 26 02 04 00 07 00 e2 07 02 0c 0c 21 17 05 00 04 00 4a 58 01 00 06 00 02 00 00 00 07 00 02 00 04 00 08 00 01 00 01 10 00 0b 00 31 35 32 31 34 30 37 30 32 39 32 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00 02 5f 00 01 00 08 00 b8 ca 41 38 41 34 37 34 02 00 04 00 7d 99 30 06 03 00 04 00 3f e2 25 02 04 00 07 00 e2 07 02 0c 0c 21 12 05 00 04 00 1e 11 03 00 06 00 02 00 06 00 07 00 02 00 03 00 08 00 01 00 01 10 00 0b 00 31 33 31 33 39 34 30 36 37 35 34 12 00 02 00 c0 b6 11 00 01 00 00 14 00 01 00 00";
//System.out.println(s.replaceAll("\s+",""));
System.out.println(decode(s));
String s1 = "这是个sb";
System.out.println(encode(s1));
System.out.println(decode(encode(s1)));
}
/**
* 解码,16进制转字符串
* @param bytes
* @return
*/
public static String decode(String bytes) {
bytes=bytes.replaceAll("\\s+","");
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
/**
* 编码字符串转16进制
* @param str
* @return
*/
public static String encode(String str) {
// 根据默认编码获取字节数组
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将字节数组中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
}
}