简体   繁体   English

在Java中通过套接字发送无符号字节

[英]Sending Unsigned Bytes Over Socket In Java

I am attempting to send the following Byte Array to a device running C. 我正在尝试将以下字节数组发送到运行C的设备。

BYTES TO SEND:
80 3f 10 01 00 3c b2 5f 0d

BYTES RECEIVED BY MACHNINE:
00 3f 10 01 00 3c 32 5f 0d

It seems for some reason that java is turning the signed bit into a 0 which is manipulating what the C Machine is reading. 出于某种原因,似乎Java会将带符号的位转换为0,从而操纵C机正在读取的内容。

80 -> 00
b2 -> 32

Here is an example of my code: 这是我的代码示例:

try
{
    String response;
    Socket clientSocket = new Socket(iPAddress, port);
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    byte[] bytes = new byte[] { (byte)0x80, 0x3f, 0x10, 0x01, 0x00, 0x3c, (byte) 0xb2, 0x5f, 0x0d};
    outToServer.write(bytes);

    response = inFromServer.readLine();   
    System.out.println("FROM SCU: " + response);
    clientSocket.close();
}
catch(Exception e)
{
    e.printStackTrace();
}

I am absolutely lost as to what I can do now as it seems nothing will work. 我完全不知道自己现在能做什么,因为似乎没有任何效果。 I do not have access to the C Machine in order to change code. 我无权访问C机器来更改代码。

For me it looks like your "C Machine" use only 7 bits. 对我来说,您的“ C Machine”似乎只使用7位。

0x80 binrary represented is 1000 0000 , and 0xB2 binary is 1011 0010 . 表示的0x80二进制是1000 0000 ,而0xB2二进制是1011 0010 If you get 7 bits from right it is 000 0000 = 0x00 and 011 0010 = 0x32 如果从右边得到7位,则为000 0000 = 0x00011 0010 = 0x32

I hope this may help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM