1

I have an app in eclipse that was written in java and an app in visual studio that is written in C#. I have a tcp connection between them.

My project sends an ID message. The message sending worked fine until now. It shows me in Visual Studio that it's sending the ID number 120 for example but when I do readbyte in eclipse it reads it as -136. As I said it worked fine last week (I didn't used it for a week and suddenly it doesn't work now). Why it is ? I don't know if it is related or not but 136+120 = 256. If I send 125 it get -131 (Every send gives 256). Please help me with it for tommorow. Thanks.

augray
  • 3,043
  • 1
  • 17
  • 30
HardQS
  • 23
  • 6
  • How do you send these IDs? In binary form or text form? If binary form is used then perhaps one side sends *unsigned* 8bit value while the other side reads *signed* 8bit value. – dlask May 30 '15 at 21:21
  • Java interprets bytes with the eight bit set as negative - this was deliberately designed that way, and has in practice been found to be useless. Convert the byte value to int and use that. – Thorbjørn Ravn Andersen May 31 '15 at 07:09

1 Answers1

0

It seems like the most significant bit in the byte is being flipped somehow. Without seeing the code there isn't much more to say. 0-127 use the first three bits, and under a signed representation, this is the max value. 128 unsigned is -127 signed under two's complement.

mr blobby
  • 64
  • 6
  • When I sending 100 it accepted good. So I guess you right the max number I can send as Byte is 128. Thanks. – HardQS May 30 '15 at 21:27
  • 127 is the max, not 128. – mr blobby May 30 '15 at 21:30
  • The real problem is that Java doesn't have support for unsigned bytes. The answer for [this question](http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back) discusses how you can convert in such a way as to be able to see the numerical values higher than 127 in an `int`. – augray May 30 '15 at 21:44