Big-Endian bit conversion

D

DIOS

I am struggling to write a converter from bits to a hex string. What I
am trying to do is to get the users inputs as far as what bits are
turned on and converting it to a hex. Problem here is that the target
system is Big-Endian so that format is the most significant bit and
byte is on the left. For example if the user selects bit-0 to ON then
the conversion is '80000000'x. Similarly if bit-4 is ON then the hex
is '08000000'x. In my documentation an example is bits 1,3,4,5,9,11,
and 17 turned on should give me a string of '5C504000'x. Im feeding
this hex string to a taget system so am not using it on the PC.

I have so far

Dim myBitArr As New BitArray(80, False)

'setup the bits that are turned ON
myBitArr(0) = True

'convert to decimal system
Dim result As Int32 = 0
For i As Int32 = 0 To myBitArr.Length - 1
If myBitArr(i) Then
result = CInt(result + System.Math.Pow(2, i))
End If
Next

Dim abyt() As Byte = BitConverter.GetBytes(result)
If BitConverter.IsLittleEndian Then
Array.Reverse(abyt)
End If

Console.WriteLine(BitConverter.ToString(abyt))

When i test this with bits 1,3,4,5,9,11, and 17 turned on I just cant
get hex equivalent of '5C504000'x
Any help is appreciated as my research into this type of conversion is
just hit a wall.

AGP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top