473,692 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

garbage Int32 out of a ByteArray

****Code Snippet***

string str = string.Format(" abc\0100\0");
byte [] b = System.Text.Enc oding.UTF8.GetB ytes(str);
System.IO.Memor yStream stm = new
MemoryStream(b, 0,b.GetLength(0 ));

BinaryReader rdr = new BinaryReader(st m);

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

Console.WriteLi ne("The SOP is
{0}",System.Tex t.Encoding.UTF8 .GetString(cls. LTI_HEADER.code ));
Console.WriteLi ne("The Message is
{0}",cls.LTI_HE ADER.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.HostT oNetworkOrder (part of this also
comes through a SOCKET interface , the text is great, the INT32's get
garbled)

.....

TIA

Feb 20 '06 #1
6 1529
Ronodev....@gma il.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.Re adInt32? It doesn't
mention parsing the string representation of a number.

Note that 3158065 = (48*256*256)+(4 8*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....@gma il.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*********@gma il.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
2053
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 this case is a gif-file. I've implemented my own class loader which extends the java ClassLoader and overrides some methods for deriving classes from the jar-bytearray. Is there anyone who have any suggestion on how to create an URL-object from...
4
12330
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
1497
by: Lou | last post by:
How do I convert a byteArray to a string?
13
12675
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 mapped to a char* in my managed-c++-function. Here is the prototype: ---------------------- managed c++ class library project ------------------------------- void MyFunction(char* buffer)
3
2492
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. What is happening is that I create an instance of this class in a separate thread and use it to call certain functions. But when I call GC.Collect (which is necessary for some timely cleanup) from the main thread, the garbage collector deletes...
1
1331
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 only 1000 items long Array(0-999), and i just want to have the positions 1000-2000 from the stream! How can i realize it that the bytes 1000-2000 are written to an Array of
2
2557
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
2201
by: gijamie911 | last post by:
int nBytes = 256; byte ByteArray = new byte; int nBytesRead = inputStream.Read(ByteArray, 0 , nBytes); recorderID = ""; for (int i = 0; i <= 14; i++) { recorderID += char.ToString((char )ByteArray);
11
4043
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 fs.Read(ByteArray, 0, fs.Length)
0
8611
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8549
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8970
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6475
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5824
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4332
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2989
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.