🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #14

Converting bytes to a numeric value

#14
From: Ron Sawyer
Date: Tue, 25 Apr 2006 18:52
13 lines
1509 bytes
I am reading 4 bytes from a socket into a byte array - and then need to convert those four bytes into a integer.  right now, I read the bytes into a byte array, convert the byte array to a string, then convert the string to an int.  There MUST be a better way.  In C/C++ I would simply do a memcpy, (roughly) like this:
Stream.Read(ByteArray, 0, 6);
memcpy(&LocalInteger, ByteArray, 4);

(Assuming that the stream had been set up and the array and the int and assuming that there were no endian issues.)

I might even do this:
Stream.Read(&LocalInteger, 0, 6);

So, how is this done is c# (I'm hating being a newbie again! :-) )

Speaking of endian issues, are there functions for swapping byte order?
Technical Details
Message-ID: <444e6fd9$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,a49c219f0a69d6f5,start
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Ron Sawyer" <win8128@aol.com>
Sender: "Ron Sawyer" <win8128@aol.com>
Reply-To: "Ron Sawyer" <win8128@aol.com>
Subject: Converting bytes to a numeric value
Newsgroups: borland.public.csharpbuilder.language.csharp
X-User-Info: 20.137.18.51     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <444e6fd9$1@newsgroups.borland.com>
Date: 25 Apr 2006 11:52:09 -0700
X-Trace: newsgroups.borland.com 1145991129 207.105.83.66 (25 Apr 2006 11:52:09 -0700)
Lines: 13
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail