garbage Int32 out of a ByteArray

R

Ronodev.Sen

****Code Snippet***

string str = string.Format("abc\0100\0");
byte [] b = System.Text.Encoding.UTF8.GetBytes(str);
System.IO.MemoryStream stm = new
MemoryStream(b,0,b.GetLength(0));

BinaryReader rdr = new BinaryReader(stm);

cls.LTI_HEADER = new Header();
cls.LTI_HEADER.code= rdr.ReadBytes(Consts.MAX_SOP);
cls.LTI_HEADER.Message = rdr.ReadInt32();

Console.WriteLine("The SOP is
{0}",System.Text.Encoding.UTF8.GetString(cls.LTI_HEADER.code));
Console.WriteLine("The Message is
{0}",cls.LTI_HEADER.Message);
***END code snippet***

i'm at my wits end as to HOW the LTI_HEADER message gets printed out as
under

---OUTPUT---
The code is abc
The Message is 3158065 <---- WHERE did '100' go ????
Press Any Key To Continue .........


---

tracing through Debug and Quickwatch - i observe that 'b' contains the
ASCII equivalents of '1', '0', '0' (ie. 49, 48, 48 in that order)

i've thought of using bitconverter into another byte array.

ive thought of using IPAddress.HostToNetworkOrder (part of this also
comes through a SOCKET interface , the text is great, the INT32's get
garbled)

.....

TIA
 
J

Jon Skeet [C# MVP]

(e-mail address removed) wrote:

cls.LTI_HEADER.Message = rdr.ReadInt32();
---OUTPUT---
The code is abc
The Message is 3158065 <---- WHERE did '100' go ????
Press Any Key To Continue .........

tracing through Debug and Quickwatch - i observe that 'b' contains the
ASCII equivalents of '1', '0', '0' (ie. 49, 48, 48 in that order)

Did you read the documentation for BinaryReader.ReadInt32? It doesn't
mention parsing the string representation of a number.

Note that 3158065 = (48*256*256)+(48*256)+49.

In other words, BinaryReader is doing exactly what it should be doing -
it's taking 4 bytes and converting them to an Int32 *assuming they're
binary*. (That's why it's called a *Binary*Reader.)

It sounds like you need to use int.Parse having grabbed the data as a
string first. I suspect you don't want to use BinaryReader at all, in
fact.

Jon
 
A

Aboulfazl Hadi

Hi
Interesting problem ! Binary Reader reads codes so we must expect it
use 49,48,48 and convert it to int as describing below

'1','0','0' ---> 49,48,48
49 (binary) 00110001
48 (binary) 00110000
48 (binary) 00110000

in reverse order

48 48 49
equals to
00110000 00110000 00110001

in decimal base it is : 3158065


Best Regards,
A.Hadi
 
R

Ronodev.Sen

.....so if i get this right .... it's taking my binary, flipping it
around and evaluating the ASCII ?

any way i get the number 100 out of this ?

Jon: im getting this out of a byte array .... and i really was explorng
BinaryReader as an alternative to chopping out fixed - lengths out of
the byte array , without getting garbled values.

Aboulfazl - so its putting the BINARY values in a LittleEndian order
and then generating the number ??
 
J

Jon Skeet [C# MVP]

....so if i get this right .... it's taking my binary, flipping it
around and evaluating the ASCII ?

No. The point is that your byte array contains an ASCII-encoded string
representation of your integer.
any way i get the number 100 out of this ?

Yes - convert the data into a string and then use int.Parse.
Jon: im getting this out of a byte array .... and i really was explorng
BinaryReader as an alternative to chopping out fixed - lengths out of
the byte array , without getting garbled values.

But although you've got a byte array, it looks like it's just
ASCII-encoded text, in which case it makes more sense to deal with it
as text.
Aboulfazl - so its putting the BINARY values in a LittleEndian order
and then generating the number ??

No. Look at the data you said you've got - your bytes are 49, 48, 48.
That's not a *binary* form of 100 at all. It's an ASCII-encoded string
of "100".

Jon
 
R

Ronodev.Sen

hi,

i used a Array.Copy, to extract 4 bytes from the 3rd position of the
byte array.

i then iterated through this byte array to cast the bytes into a char,
and then added this to a stringbuilder (ignored the 0's)

this returns me a string with the value 'd'.

now what i DO want is the #100 (ASCII 'd')...

do i just cast this back to int ?

doing int.Parse() throws an "Input String Is Not In Correct Format"
BUT obviously cos this is not a valid NUMERIC value.

how i miss (int)char in C++ (i never thought i'd see that day i'd MISS
C++/C :) )

any thoughts , people?
 
J

Jon Skeet [C# MVP]

i used a Array.Copy, to extract 4 bytes from the 3rd position of the
byte array.

i then iterated through this byte array to cast the bytes into a char,
and then added this to a stringbuilder (ignored the 0's)
this returns me a string with the value 'd'.

It shouldn't. It should return "100". That's what you want. However, a
much easier way of doing this is just to use Encoding.ASCII.GetString.
You can then use int.Parse to parse it.
how i miss (int)char in C++ (i never thought i'd see that day i'd MISS
C++/C :) )

That wouldn't have really helped you though in this case - at least, it
might have helped you to do it a bad way, but not the best way.

Jon
 
Top