简体   繁体   English

Java-通过TCP连接发送无符号字节

[英]Java - Sending unsigned bytes through TCP connection

Since Java bytes are signed values and I'm trying to establish a TCP socket connection with a C# program that is expecting the bytes to be unsigned. 由于Java字节是带符号的值,因此我试图与C#程序建立TCP套接字连接,该程序期望字节是无符号的。

I am not able to change the code on the C# portion. 我无法更改C#部分上的代码。

How can I go about sending these bytes in the correct format using Java. 如何使用Java以正确的格式发送这些字节。

Thanks 谢谢

No, Java bytes are signed values. 不,Java字节是带符号的值。 In general C# bytes are unsigned . 通常,C#字节是无符号的 (You'd need the sbyte type to refer to signed bytes; I can't remember the last time I used sbyte .) (您需要sbyte类型来引用带符号的字节;我不记得上次使用sbyte 。)

However, it shouldn't matter at all in terms of transferring data across the wire - normally you just send across whatever binary data you've got (eg what you've read from a file) and both sides will do the right thing. 但是,就通过电线传输数据而言,这根本不重要-通常,您只是发送所拥有的任何二进制数据(例如,从文件中读取的数据),并且双方都将做正确的事情。 A byte with value -1 on the Java side will come through as a byte with value 255 on the C# side. Java端的值为-1的字节将作为C#端的值为255的字节通过。

If you can tell us more about exactly what you're trying to do (what the data is) we may be able to help more, but I strongly suspect you can just ignore the difference in this case. 如果您可以确切地告诉我们您要做什么(数据是什么),我们也许可以提供更多帮助,但是我强烈怀疑您在这种情况下可以忽略两者之间的区别。

It doesn't matter. 没关系 The numbers are just strings of bits and the fact that they're signed or unsigned doesn't matter as far as the bits are concerned. 数字只是一串位,就位而言,它们是带符号或无符号的事实无关紧要。 And it's the bits that are transferred so all that signed/unsigned information is irrelevant. 而且是要传输的位,因此所有已签名/未签名的信息都不相关。

1100101010101111 is still 1100101010101111 regardless of signed/unsigned. 不管有符号/无符号, 1100101010101111仍然是1100101010101111

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

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