garbled long value returned from the network

  • Thread starter Thread starter family.sens
  • Start date Start date
F

family.sens

hi all,

im using a C# webservice to communicate some data over a socket to a
VC++ program.

what the vc++ program does is return a struct() over the socket.

now the struct contains 2 32 bit integers (according to VC++) i.e. long
(System.Int32)

all the byte[] data is received correctly, however , if im expecting
'320' as the value of the long variable im getting a huge number like
'612334539'.

does this have something to do with Little-Endian / Big-Endian issues ?


does this require me to use IPAddress.NetworkToHostOrder() ?

TIA.

Neil
 
im using a C# webservice to communicate some data over a socket to a
VC++ program.

what the vc++ program does is return a struct() over the socket.

now the struct contains 2 32 bit integers (according to VC++) i.e. long
(System.Int32)

Note that a long may be 32 bits in C (in some compilers anyway) but in
C# an int is 32 bits and a long is 64.
all the byte[] data is received correctly, however , if im expecting
'320' as the value of the long variable im getting a huge number like
'612334539'.

does this have something to do with Little-Endian / Big-Endian issues ?

Sounds like it.
does this require me to use IPAddress.NetworkToHostOrder() ?

No. You can use my EndianBitConverter, available at
http://www.pobox.com/~skeet/csharp/miscutil

That lets you convert in either endianness. (There's an equivalent
EndianBinaryReader/Writer, too.)
 
thank you John, i will try that when i get back on Monday..

the client at the other end is VC++ 6.0 .... running a sizeof() on a
long variable at that end confirms its 32 bits (4 bytes) in length.

my C# program is using System.Int32 , and not 'long' :) thanks for that
heads-up too .... its a common confusion i believe.

have a good weekend, mate.
 
Jon:have yet to give your EndianBitConverter a try ...

Everyone : tried determining Endianness, then

doing a Array.Reverse() on my byte array and then doing a
BitConverter.ToInt32.

the value that i was getting before turns -/ve.

interesting fact i found out is

(decimal - 10486155, hex - a0018b) {139, 1 , 160 , 0} (which were the
four numbers i was getting in my byte[])

ONLY the last 2 (160, 0 ) is correct .... i have NO clue where the
139,1 is coming from.

i wasnt aware that System.Net.Sockets appends any sort of High-Order
Byte ....

forgive me i've just been thinking of this problem so much i KNOW Im
missing the forest for the trees!!!

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top