有问题请教,
String a = 4A 52 2D 56 44 4D 2D 31 01 B9 02 FD 77 02 03 00 48 31 2B F0 F0 D5 94
前8字节要转化成ASCII码,后面试16进制数,要转化成10进制数,每个字节意义不同。java里怎么处理这个的?JavaStringascii16进制字符串

解决方案 »

  1.   

    主楼说的问题,这样,
    public static void main(String[] args) {
    String input = "4A 52 2D 56 44 4D 2D 31 01 B9 02 FD 77 02 03 00 48 31 2B F0 F0 D5 94";
    byte[] byteArray = hexStringToBytes(input);

    int asciiSize = 8;
    StringBuffer asciiValue = new StringBuffer();
    for (int i = 0; i < asciiSize; i++)
    asciiValue.append((char) byteArray[i]);
    System.out.println(asciiValue.toString());

    StringBuffer intValue = new StringBuffer();
    for (int i = asciiSize; i < byteArray.length; i++) {
    if (i == asciiSize)
    intValue.append((int) byteArray[i]);
    else
    intValue.append(",").append((int) byteArray[i]);
    }
    System.out.println(intValue.toString());
    } /**
     * 十六进制字符串转换为字节数组
     * @param hexString 十六进制字符串
     * @return 字节数组
     */
    public static byte[] hexStringToBytes(String hexString) {
    String[] hexArray = hexString.split(" ");
    int length = hexArray.length;
    byte[] bytes = new byte[length];
    for (int i = 0; i < length; i++) {
    String hex = hexArray[i];
    int valueInt = Integer.parseInt(hex, 16);
    bytes[i] = (byte) valueInt;
    }
    return bytes;
    }