Re: How to Convert Byte Array to Int32 (Big-endian)
From: Charles Law (blank_at_nowhere.com)
Date: 01/22/05
- Next message: poldoj: "Re: Asp.net: detect textbox scrolling"
- Previous message: Herfried K. Wagner [MVP]: "Re: Reset a form?"
- In reply to: Dennis: "RE: How to Convert Byte Array to Int32 (Big-endian)"
- Next in thread: Gerald Hernandez: "Re: How to Convert Byte Array to Int32 (Big-endian)"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 22 Jan 2005 00:06:10 -0000
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" <Dennis@discussions.microsoft.com> wrote in message
news:AA34156E-F28D-4F92-9C17-38DA9EFDC5E9@microsoft.com...
> Here's a couple of functions that I use...probably a better way to do it
> but
> it works:
>
> Friend Function ConvIntegertoByteArray(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 ConvByteArraytoInteger(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
>>
>>
>>
- Next message: poldoj: "Re: Asp.net: detect textbox scrolling"
- Previous message: Herfried K. Wagner [MVP]: "Re: Reset a form?"
- In reply to: Dennis: "RE: How to Convert Byte Array to Int32 (Big-endian)"
- Next in thread: Gerald Hernandez: "Re: How to Convert Byte Array to Int32 (Big-endian)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|