简体   繁体   中英

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.

I am not able to change the code on the C# portion.

How can I go about sending these bytes in the correct format using Java.

Thanks

No, Java bytes are signed values. In general C# bytes are unsigned . (You'd need the sbyte type to refer to signed bytes; I can't remember the last time I used 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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