hello...
First of all, I am not sure if this is the right place to post this...
anyway, here's my prob...
I am writing a TCP server in C#...
and a client in JAVA.....
I converted an int in JAVA into byte[4]...(tested it in java, nothing wrong).
sent it over to the server..
At the server, convert it back into int from byte[4]....and it becomes 16777216 instead of the original number which is 1..
my question is, is it possible that JAVA and C# has different formatting in the conversion or is it that my reader or writer of the server and client that has the problem I do manage to send text correctly over the network...
here's my code...
JAVA
myByte= ByteBuffer.allocate(4);
myByte.putInt( myInt);
myByte.flip();
mySocketChannel.write(myByte);
C#
Byte
[] bytes = new Byte[4];stream.Read(bytes, 0, bytes.Length);
int value = BitConverter.ToInt32(bytes, 0);
thanks in advance...

BitConverter
Andy Tso
Probably the java program is running on a computer/operating system that uses a different endianess (is that correct ;)) than your windows computer. Our standard windows computer works with little endian while some unix systems work with big endians, i.e. you have to flip the byte order arround.
If I take alook at the bytes from the number 16777216, it looks like this:
00-00-00-01
but to be "1" it should be this:
01-00-00-00
Which lead me to my suspicion...
jX
was thinking to have a quick way...'coz I got quite a few to add...
thanks ....
Shanavas
Thanks for the clue....
actually both client and server are running on the same system, so it's probably not that issue...(However, I might have them running on diff sys in the future, that's gonna be something to look into..:P)
and you are right....it turn out to be 00-00-00-01 when i look at it as string...but i did switch it from byte to int on client before sending it....it's nothing wrong....only after sending it over it becomes like that...
do you have any idea how can I convert byte to int other than using BitConverter
John Morales
Use IPAddress static methods: HostToNetworkOrder() or NetworkToHostOrder() to switch from litlle endian to big endian and vice-versa.
ex.:
int tempData = IPAddress.HostToNetworkOrder((int)data); byte[] temp = BitConverter.GetBytes(tempData);Maikel56450
Well ;) As always, add it up:
int x = (bytes[3] << 24) + (bytes[2] << 16) + (bytes[1] << 8) + bytes[0];You just have to know the right byte order, I took them now, switch if you must..