如何将 40 31 30 30 31 01 43 38 00 18 00 00 00 64 A0 0B A0 2A EA 54 00 0A BA 95 00 00 87 00 01 D6 1F 01 26 8C CA 0D
以 0x40,0x31,0x30,0x30,0x31,0x01,0x43,0x38,0x00,0x18,0x00,0x00,0x00,0x64,0xA0,0x0B,0xA0,0x2A,0xEA,0x54,0x00,0x0A,0xBA,0x95,0x00,0x00,0x87,0x00,0x01,0xD6,0x1F,0x01,0x26,0x8C,0xCA,0x0D
格式保存在byte[] 中啊 ,谢谢大家,急啊 !!
以 0x40,0x31,0x30,0x30,0x31,0x01,0x43,0x38,0x00,0x18,0x00,0x00,0x00,0x64,0xA0,0x0B,0xA0,0x2A,0xEA,0x54,0x00,0x0A,0xBA,0x95,0x00,0x00,0x87,0x00,0x01,0xD6,0x1F,0x01,0x26,0x8C,0xCA,0x0D
格式保存在byte[] 中啊 ,谢谢大家,急啊 !!
public static void main(String args[]){
String hexStr="40 31 30 30 31 01 43 38 00 18 00 00 00 64 A0 0B A0 2A EA 54 00 0A BA 95 00 00 87 00 01 D6 1F 01 26 8C CA 0D";
String[] hexArr=hexStr.split("\\s");
byte b[]=new byte[hexArr.length];//输出用的字节数组组
for(int i=0;i<hexArr.length;i++){
b[i]=(byte)(Integer.parseInt(hexArr[i],16));
System.out.println(Integer.toHexString(b[i]));
}
}
String str = "40 31 30 30 31 01 43 38 00 18 00 00 00 64 A0 0B " +
"A0 2A EA 54 00 0A BA 95 00 00 87 00 01 D6 1F 01 " +
"26 8C CA 0D";
byte[] bys = toBytes(str);
System.out.println(ByteUtil.bytes2StrSpace(bys));
} private static byte[] toBytes(String str) {
if(str == null) {
return null;
}
return parse(str);
} private static byte[] parse(String str) {
byte[] bys = new byte[str.length() / 3 + 1];
char[] chs = str.trim().toLowerCase().toCharArray();
boolean isStart = false;
int offset = 0;
int n = 0;
for(int i = 0; i < chs.length; i++) {
if(isHexChar(chs[i])) {
if(!isStart) {
isStart = true;
}
n = n * 16 + hex(chs[i]);
continue;
}
if(!isStart) {
continue;
}
isStart = false;
bys[offset++] = (byte)n;
n = 0;
}
if(isStart) {
bys[offset++] = (byte)n;
}
byte[] out = new byte[offset];
System.arraycopy(bys, 0, out, 0, offset);
return out;
} private static int hex(char c) {
if(c >= '0' && c <= '9') {
return c - '0';
}
if(c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
if(c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
return -1;
} private static boolean isHexChar(char c) {
if(c >= '0' && c <= '9') {
return true;
}
if(c >= 'a' && c <= 'f') {
return true;
}
if(c >= 'A' && c <= 'F') {
return true;
}
return false;
}
}class ByteUtil { private final static char[] HEX = "0123456789abcdef".toCharArray(); private ByteUtil() { } public static String bytes2StrSpace(byte[] bys) {
char[] chs = new char[bys.length * 3 - 1];
for(int i = 0, k = 0; i < bys.length; i++) {
if(k > 0) {
chs[k++] = ' ';
}
chs[k++] = HEX[(bys[i] >> 4) & 0xf];
chs[k++] = HEX[bys[i] & 0xf];
}
return new String(chs);
}
}