471,310 Members | 1,026 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

garbled long value returned from the network

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

Feb 18 '06 #1
3 1035
<fa*********@gmail.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 18 '06 #2
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.

Feb 18 '06 #3
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

Feb 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by steve | last post: by
11 posts views Thread by Daniel Wilcox | last post: by
3 posts views Thread by John Dalberg | last post: by
6 posts views Thread by Bruce W.1 | last post: by
4 posts views Thread by Lingyun Yang | last post: by
36 posts views Thread by Digital Puer | last post: by
12 posts views Thread by Ahmad Jalil Qarshi | last post: by
105 posts views Thread by Keith Thompson | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.