java字符串转换为十六进制数组

java字符串转换为十六进制数组
可不可以把比如字符串 "55010100AAFF69" 转成十六进制 0x55, 0x01, 0x01, 0x00, 0x00, 0xAA, 0xFF, 0x69,后放入Byte数组...
十六进制格式存储好扮敬说,主要还是要看你这厅知慎种是怎么拆分,比如5 50 10 10 0A AF F6 9或5 5 0 1 0 1 0 0 A A F F 6 9或…猛唯…

  private Byte[] test(String s) {
    int len = s== null ? 0 : s.length() / 2;
    Byte[] bs = new Byte[len];
    for ( int i = 0; i < len; i ++ ) {
      String hex = s.substring(i*2, i*2+2);
      旅裂int 陪销num = Integer.parseInt(hex, 16);
      bs[i] = new Byte((byte)num);
    芦镇游}
    return bs;
  }

如果需要的是byte数组,那么直接将int强制转为byte就行了。如果直接用Byte的parse,需要处理异常。


字辩消符串闭大作轿灶竖为函数change的参数inputStr
byte[] change(String inputStr) {
byte[] result = new byte[inputStr.length() / 2];
for (int i = 0; i < inputStr.length() / 2; ++i)
result[i] = (byte)(Integer.parseInt(inputStr.substring(i * 2, i * 2 +2), 16) & 0xff);
return result;
}
import java.util.Scanner;
String source="55010100AAFF69);

sc=new Scanner(source);
byte [] Byte = new byte[10];
int i=0;

while (sc.hasNextByte(16)) Byte[i++]=sc.NextByte(16);
← 上一篇: 下一篇: →