473,624 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Convert Byte Array to Int32 (Big-endian)

I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?

TIA

Charles
Nov 21 '05 #1
14 29709
Charles,

We have a long time not seen you, so welcome back,

Maybe a crazy idea for you, however multiplying and adding?

Cor
Nov 21 '05 #2
"Charles Law" <bl***@nowhere. com> schrieb:
I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?


You may want to reverse the byte array before using 'BitConverter'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3
Hi Cor

I haven't really been away, just a bit quiet. I did respond to someone the
other day about a serial comms issue crashing their prog, but no word yet
whether it fixed the problem.
Maybe a crazy idea for you, however multiplying and adding?
Yes, that is currently what I do, or rather what the VB6 code that I am
converting does. I was just looking for a native .NET way to do it. It seems
to me that there should be a switch, or extra parameter to pass that allows
one to specify big or little-endianness, but I haven't spotted it.

Charles
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. Charles,

We have a long time not seen you, so welcome back,

Maybe a crazy idea for you, however multiplying and adding?

Cor

Nov 21 '05 #4
Hi Herfried

As I have just replied to Cor, I kind of expected a switch somewhere to
allow me to specify the endianness of the operation, but it seems to be
fixed.

I had toyed with reversing the bytes, but then I might just as well stick to
carving the array up by hand. Besides, it makes it a two-stage process, so
to avoid duplication I would need a function wrapper.

Perhaps I am just being picky, but I like to use built-in stuff wherever
possible; or maybe it's laziness ;-)

Charles
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:uU******** ******@TK2MSFTN GP14.phx.gbl...
"Charles Law" <bl***@nowhere. com> schrieb:
I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?


You may want to reverse the byte array before using 'BitConverter'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
Charles,
Jon Skeet has a EndianBitConver ter at
http://www.yoda.arachsys.com/csharp/miscutil/ that works similar to
BitConverter but either Little-Endian or Big-Endian depending on which you
pick...

Its in C#, however you should be able to use directly as a class assembly,
or easily converted to VB.NET as the source is available...

Hope this helps
Jay

"Charles Law" <bl***@nowhere. com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?

TIA

Charles

Nov 21 '05 #6
Here's a couple of functions that I use...probably a better way to do it but
it works:

Friend Function ConvIntegertoBy teArray(ByVal n As Long, ByVal lg As
Integer) As Byte()
'converts an integer to a byte array of length lg
Dim m() As Byte = New Byte(lg - 1) {}
Dim i, k As Integer
Dim h As String
h = Hex(n).PadLeft( 16, "0"c)
k = 16
For i = lg - 1 To 0 Step -1
k = k - 2
m(i) = CByte("&H" & h.Substring(k, 2))
Next
Return m
End Function

Public Function ConvByteArrayto Integer(ByVal b As Byte(), Optional ByVal
ln As Integer = 0, Optional ByVal sidx As Integer = 0) As Long
Dim i As Integer
Dim j, k As Long
If ln = 0 Then ln = UBound(b) + 1
ln = sidx + ln - 1
k = 1
j = CInt(b(ln))
For i = ln - 1 To sidx Step -1
k = 256 * k
j = j + k * b(i)
Next
Return j
End Function

"Charles Law" wrote:
I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?

TIA

Charles

Nov 21 '05 #7
Hi Dennis

Thanks for the reply. As I said in a couple of other replies, it is not so
much the ability to manipulate the data by hand that eludes me, but the
native .NET way, if any. I suppose I am jut trying to avoid re-inventing the
wheel if, indeed, such a wheel exists.

Charles
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:AA******** *************** ***********@mic rosoft.com...
Here's a couple of functions that I use...probably a better way to do it
but
it works:

Friend Function ConvIntegertoBy teArray(ByVal n As Long, ByVal lg As
Integer) As Byte()
'converts an integer to a byte array of length lg
Dim m() As Byte = New Byte(lg - 1) {}
Dim i, k As Integer
Dim h As String
h = Hex(n).PadLeft( 16, "0"c)
k = 16
For i = lg - 1 To 0 Step -1
k = k - 2
m(i) = CByte("&H" & h.Substring(k, 2))
Next
Return m
End Function

Public Function ConvByteArrayto Integer(ByVal b As Byte(), Optional
ByVal
ln As Integer = 0, Optional ByVal sidx As Integer = 0) As Long
Dim i As Integer
Dim j, k As Long
If ln = 0 Then ln = UBound(b) + 1
ln = sidx + ln - 1
k = 1
j = CInt(b(ln))
For i = ln - 1 To sidx Step -1
k = 256 * k
j = j + k * b(i)
Next
Return j
End Function

"Charles Law" wrote:
I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?

TIA

Charles

Nov 21 '05 #8
Hi Jay

Thanks for the link. I have had a look and I see that Jon uses, in essence,
the same approach that I have at present. I guess that is also how the .NET
little endian BitConverter method is coded under the covers, so it seems
that my best bet is just to wrap the functions I have a bit better and leave
it at that.

Cheers.

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:O3******** ******@TK2MSFTN GP15.phx.gbl...
Charles,
Jon Skeet has a EndianBitConver ter at
http://www.yoda.arachsys.com/csharp/miscutil/ that works similar to
BitConverter but either Little-Endian or Big-Endian depending on which you
pick...

Its in C#, however you should be able to use directly as a class assembly,
or easily converted to VB.NET as the source is available...

Hope this helps
Jay

"Charles Law" <bl***@nowhere. com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?

TIA

Charles


Nov 21 '05 #9
Charles,
I would expect yours & Jon's approach to be very similar.

My point is that Jon has done all the "leg work" & created "complete"
versions of the classes. In other words Why reinvent the wheel?

Hope this helps
Jay

"Charles Law" <bl***@nowhere. com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi Jay

Thanks for the link. I have had a look and I see that Jon uses, in
essence, the same approach that I have at present. I guess that is also
how the .NET little endian BitConverter method is coded under the covers,
so it seems that my best bet is just to wrap the functions I have a bit
better and leave it at that.

Cheers.

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:O3******** ******@TK2MSFTN GP15.phx.gbl...
Charles,
Jon Skeet has a EndianBitConver ter at
http://www.yoda.arachsys.com/csharp/miscutil/ that works similar to
BitConverter but either Little-Endian or Big-Endian depending on which
you pick...

Its in C#, however you should be able to use directly as a class
assembly, or easily converted to VB.NET as the source is available...

Hope this helps
Jay

"Charles Law" <bl***@nowhere. com> wrote in message
news:OD******** ******@TK2MSFTN GP11.phx.gbl...
I thought this had come up before, but I now cannot find it.

I have a byte array, such as

Dim a() As Byte = {1, 2, 3, 4}

I want to convert this to an Int32 = 01020304 (hex).

If I use BitConverter, I get 04030201.

Is there a built-in way to do this, or am I stuck with extracting and
shifting each byte manually?

TIA

Charles



Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
15307
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing the another client in C#. Server side can not be changed :( So, I am using tcp/ip sockets. In the end I get byte on the client side, which originally was some C++ class object, converted to byte array.
5
134745
by: Andrew Inwards | last post by:
Can anyone tell me how to convert a byte array to a stream.? Thanks Andrew
4
33122
by: Dan | last post by:
I need to convert a byte array to a string in order to upload a binary file with an httpWebRequest. What's the most efficient way to do such a conversion?
2
9605
by: Dave | last post by:
Hi, I'm trying to convert a byte array to string --This works... System.BitConverter.ToString(bytes) "EB-55-79-20-18-B2-76-4D-85-0A-93-6B-97-33-31-B8" --This doesn't, but returns "System.Byte". How do I do this with System.Convert.ToString()???
5
4191
by: Terry Olsen | last post by:
Looking for info on how to convert a byte array to a string, and string to byte array. Thanks.
0
2410
by: Sergei Shelukhin | last post by:
Hi. I receive a byte array that contains utf-16 string (with a lot of \0 characters :)) from a legacy system that holds some data that is changed elsewhere. How do I convert it to an actual utf-16 string for my app's purposes? I am using php 5.
18
43039
by: MrVS | last post by:
Hi, I have a C++ CLR class method that takes System::Byte *b as parameter argument. I want the CSharp caller pass a byte * to this function. But in the CSharp prorgram, I only managed to create a byte array (byte). Can anyone tell me how to convert byte array to byte pointer in CSharp? Please show me an simple example about how to do it. Thank you for your help
1
4723
by: Chintan Shah | last post by:
I have a byte array which looks something like 01 00 02 00 73 45 69 A5 So i have to read first 2 bytes and convert it into integer to get its value. Same for next 2 bytes and then next 4 bytes. Here the value for first 2 bytes (01 00) is 1, next 2 bytes (02 00) is 2. So could some one help me on this.
0
8174
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
8680
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
8624
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...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7164
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
6111
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
4082
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
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.