Java-将十六进制字符串转换为字节数组并返回

问题描述 投票:0回答:1

我需要将作为参数传递的十六进制字符串转换为设备(BLE),该设备将数据转换为byte [],并且它的响应必须转换回十六进制字符串。

必须作为参数传递给设备的十六进制字符串为260307FF2F

我通常在StackOverflow和整个Internet上进行搜索,没有任何解决方案能够为我提供预期的结果。

但是,我怎么知道预期的结果?我使用了一个名为LightBlue的应用程序连接到我的设备,它返回了以下十六进制字符串:270407CC00FE

将十六进制字符串作为参数传递给设备的我的方法:

public void returnData(String data) {
    data = "260307FF2F"; //this data has a fixed value

    // some Bluetooth implementation

    byte[] hexToByteData = data.getBytes();

    //Some more bluetooth implementation, passing hexToByteData as parameter to the BLE device
}

现在,是对响应数据进行解码的方法。

public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { 
    // gatt and characteristic are ways to communicate with BLE devices, 
    // characteristic holds the return value, as you can see below

    byte [] dataByte = characteristic.getValue();

    String hexString = bytesToHex(dataByte);
    hexString = hexString.toUpperCase();

    System.out.println("Characteristic Hex Value: " + hexString);

}

bytesToHex()方法,将byte[]解码回十六进制字符串:

private static String bytesToHex(byte[] hashInBytes) {

    StringBuilder sb = new StringBuilder();
    for (byte b : hashInBytes) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

所以,现在是实际问题:

当我按原样使用returnData method()时,调用data.getBytes(),结果是其他十六进制字符串,而不是上述所需的270407CC00FE。如何正确转换作为参数(260307FF2F)传递的十六进制字符串,以便获得所需的收益?

java arrays hex byte
1个回答
0
投票

我尝试了很多在SO和互联网上发现的方法。我将发布一些示例。

这个:

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 
        + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

从这里得到:https://stackoverflow.com/a/140861/11703579

这个:

public static String toHex(byte [] buf) {
    StringBuffer strbuf = new StringBuffer(buf.length * 2);
    int i;
    for (i = 0; i < buf.length; i++) {
        if (((int) buf[i] & 0xff) < 0x10) {
            strbuf.append("0");
        }
        strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    }
    return strbuf.toString();
}

从这里:In Java, how do I convert a hex string to a byte[]?

还有这个:

    public String toHex(String arg) {
        return String.format("%040x", new BigInteger(1, arg.getBytes(/*CHARSET*/)));
    }

从这里:https://stackoverflow.com/a/2149927/11703579

每个人,给我一个不同的结果。没有人得到我预期的结果。

顺便说一下,这些方法应用于returnData(),使用String data作为源并应用更改

经过一天多的努力,这就是窍门:

public void returnData(String data) {
    // some BLE stuff
    byte[] hexData = sendByteData(data); 
    // some BLE stuff
}

救生方法:

public static byte[] sendByteData(String mensagem)
{
    byte[] sendingThisByteArray = new byte[mensagem.length()/2];
    int count  = 0;

    for( int i = 0; i < mensagem.length() -1; i += 2 )
    {
        String output;
        output = mensagem.substring(i, (i + 2));
        int decimal = (int)(Integer.parseInt(output, 16));

        sendingThisByteArray[count] =  (byte)(decimal & 0xFF);
        count ++;
    }
    return sendingThisByteArray;
}

就是这样,希望它可以帮助其他人将十六进制/字符串转换为字节然后返回。

© www.soinside.com 2019 - 2024. All rights reserved.