471,353 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

garbage Int32 out of a ByteArray

****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_H EADER.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

Feb 20 '06 #1
6 1466
Ronodev....@gmail.com wrote:

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

Feb 20 '06 #2
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

Feb 20 '06 #3
.....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 ??

Feb 21 '06 #4
Ronodev....@gmail.com wrote:
....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

Feb 21 '06 #5
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?

Feb 22 '06 #6
Ro*********@gmail.com wrote:
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

Feb 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kristina Engdahl | last post: by
4 posts views Thread by Chris | last post: by
2 posts views Thread by Lou | last post: by
13 posts views Thread by Ekim | last post: by
3 posts views Thread by Stephen Engle | last post: by
1 post views Thread by Daniel von Fersen | last post: by
2 posts views Thread by minorguy | last post: by
reply views Thread by XIAOLAOHU | 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.