Android蓝牙和发送未签名的十六进制值

Android蓝牙和发送未签名的十六进制值,android,bluetooth,byte,bytearray,unsigned-integer,Android,Bluetooth,Byte,Bytearray,Unsigned Integer,我目前正在开发一个Android应用程序,它从文本框中获取值,然后通过蓝牙发送,所有操作都是十六进制值 我有一个转换方法,它可以接受字符串make,给我字符串的无符号整数,但是一旦我把它放在字节数组中,它就变成有符号的,并且接收它的板不能执行有符号十六进制 这就是过程的工作原理: //sample string to send String toSend = "0BDD"; //sending the byte[] to the board over bluetooth btOutputStr

我目前正在开发一个Android应用程序,它从文本框中获取值,然后通过蓝牙发送,所有操作都是十六进制值

我有一个转换方法,它可以接受字符串make,给我字符串的无符号整数,但是一旦我把它放在字节数组中,它就变成有符号的,并且接收它的板不能执行有符号十六进制

这就是过程的工作原理:

//sample string to send
String toSend = "0BDD";

//sending the byte[] to the board over bluetooth
btOutputStream.write(SendByteData(toSend));

// --- perform the conversion to byte[] ---
public static byte[] SendByteData(String hexString)
{
    byte[] sendingThisByteArray = new byte[hexString.length()/2];
    int count  = 0;

    for( int i = 0; i < hexString.length() - 1; i += 2 )
    {
        //grab the hex in pairs
        String output = hexString.substring(i, (i + 2));

        //convert the 2 characters in the 'output' string to the hex number
        int decimal = (int)(Integer.parseInt(output, 16)) ;

        //place into array for sending
        sendingThisByteArray[count] =  (byte)(decimal);

        Log.d(TAG, "in byte array = " + sendingThisByteArray[count]);
        count ++;
    }

    return sendingThisByteArray;
}
11被正确放置在sendingThisByteArray[0]中 但是对于发送此ByteArray[1],数字221变为-35

我知道Java有签名字节。。是否有方法放置/放置/更改字节数组,以便我可以放置和编号221或任何其他高于127的值


非常感谢您的帮助

您可以通过二进制和0xFF将有符号整数转换为无符号字节,如下所示:

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

通过这种方式,您可以发送0到255之间的值。我发现了问题,当我尝试在字节数组中使用无符号十六进制时,它总是会放一个符号,我必须创建单个字节来保留无符号十六进制

intzeroa=(int)(Integer.parseInt(“0D”,16));
write(unsignedToBytes((byte)zeroA))

它肯定能工作,所以故障在于正在进行重新解释的任何东西,无论是调试代码,还是蓝牙设备上的逻辑,或者两者兼而有之。最后想一想,你到底在哪里看到“-35”?当使用“&0xFF”时,如果通过蓝牙显示“-35”值,那么它已经解释了完整的256个值,您只能得到-128到+127。也许你可以详细说明你所说的“董事会不能做”是什么意思?
sendingThisByteArray[count] =  (byte)(decimal & 0xFF);