473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1517
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kristina Engdahl | last post by:
Hello, I'm working with dynamic classloading and have encountered a problem when it comes to defining an URL for a file that is available through a bytearray representing a jar-file. The file in...
4
by: Chris | last post by:
Hi, I think I'm having some problems here with garbage collection. Currently, I have the following code: public struct Event { public int timestamp;
2
by: Lou | last post by:
How do I convert a byteArray to a string?
13
by: Ekim | last post by:
hy to all, I'm accessing a function from a managed-c++-dll within my c#-application. Therefore I encountered following troubles: In my c#-app I have got a byte-array, which should somehow be...
3
by: Stephen Engle | last post by:
I have an app that I am working on that is dependent on an unmanaged code library. There is a middle library of managed C++ that encapsulates the unmanaged libary functions calls into a class. ...
1
by: Daniel von Fersen | last post by:
When I want to Read the Bytes 1000-2000 from a Stream into a ByteArray using Stream.Read(byteArray,1000,2000) they are written to the positions 1000-2000 in the byteArray. but my Array is...
2
by: minorguy | last post by:
1.) In C++/CLI, I have the following structures defined, which work as unions: public ref struct One { Int32 a; };
1
by: gijamie911 | last post by:
int nBytes = 256; byte ByteArray = new byte; int nBytesRead = inputStream.Read(ByteArray, 0 , nBytes); recorderID = ""; for (int i...
11
by: Icemokka | last post by:
Hi, I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL 2005. My code looks like this : fs = New FileStream(sFilePath, FileMode.Open) Dim ByteArray(fs.Length) As Byte...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.