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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Charles Law (blank_at_nowhere.com)
Date: 01/22/05


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
>>
>>
>>



Relevant Pages

  • Re: That tricky 2nd Uni - 26" or 29"
    ... serious offroad, 125s are a good comprimise for control, speed and hill ... of your cranks to your wheel size. ... ability, and then get a set of 125mm or 101mm cranks for road riding if ...
    (rec.sport.unicycling)
  • Re: SCWs legacy
    ... >Charles wrote: ... >>>Perhaps you need to reread what you posted! ... as I have the ability to commit such profundity to ... I ment evry werd of it mate! ...
    (rec.sport.rugby.union)
  • Re: Strange command histories in hacked shell history
    ... I like the idea of being able to allow certain users to ability to ... utilize one privileged task while not granting that user the ability to ... don't trust to many to be in my wheel and my favorite practice is ... then they should not be able to su to root. ...
    (FreeBSD-Security)
  • Re: Satellite system
    ... >> Wrong predictions - for example, that the wheel would become obsolete, ... > but the hovercraft does work an there is the "Maglev" transportation system ... Yes, Charles, but that's missing the point! ...
    (uk.tech.digital-tv)
  • Re: Vibrating grinder returns
    ... The fact that side wobble changes as the wheel rotates on the shaft is a good indication that the flanges are not at a right angle. ... If they are a slop fit, and free to wobble side to side, you'd be very well served to machine new ones that restrict that ability, but they MUST run true, otherwise you run the risk of cracking the wheel. ... I flattened these flange washers up by hand, and added a machined and hardened little washer to extend the shoulder, but still. ...
    (rec.crafts.metalworking)