469,900 Members | 1,513 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,900 developers. It's quick & easy.

Convert BitArray to hexadecimal string

Maybe I'm just easily baffled after an all-nighter but I can't seem to
figure out how to represent a BitArray as a hexadecimal string.

For example:

Dim outputBank As New BitArray(8)

outputBank(0) = True
outputBank(1) = False
outputBank(2) = False
outputBank(3) = True
outputBank(4) = True
outputBank(5) = False
outputBank(6) = True
outputBank(7) = False

The hexadecimal string for the above array of bits would be "59". How can
I obtain that string? Is there an easy built-in method for doing this or
do I have to build something on my own?

Joel Moore
Jul 21 '05 #1
2 8519
I first converted the BitArray to an Int32.

Dim result As Int32 = 0

For i As Int32 = 0 To outputBank.Length - 1
If outputBank(i) Then
result += System.Math.Pow(2, i)
End If
Next

MessageBox.Show(result.ToString("X"))

More info on formatting strings:
http://msdn.microsoft.com/library/de...matstrings.asp
"Joel Moore" <as*******@asdaadad.com> wrote in message
news:Xn******************************@207.46.248.1 6...
Maybe I'm just easily baffled after an all-nighter but I can't seem to
figure out how to represent a BitArray as a hexadecimal string.

For example:

Dim outputBank As New BitArray(8)

outputBank(0) = True
outputBank(1) = False
outputBank(2) = False
outputBank(3) = True
outputBank(4) = True
outputBank(5) = False
outputBank(6) = True
outputBank(7) = False

The hexadecimal string for the above array of bits would be "59". How can
I obtain that string? Is there an easy built-in method for doing this or
do I have to build something on my own?

Joel Moore

Jul 21 '05 #2
I wound up doing this:

Dim temp As Byte
Dim hexstring As String

temp = (Convert.ToByte(outputBank(7)) << 7) + _
(Convert.ToByte(outputBank(6)) << 6) + _
(Convert.ToByte(outputBank(5)) << 5) + _
(Convert.ToByte(outputBank(4)) << 4) + _
(Convert.ToByte(outputBank(3)) << 3) + _
(Convert.ToByte(outputBank(2)) << 2) + _
(Convert.ToByte(outputBank(1)) << 1) + _
Convert.ToByte(outputBank(0))

hexstring = Format(temp, "X2")

Which seems to give me what I need. Not sure how (in)efficient it is,
though.

Thanks for the reply.

"Andy Gaskell" <pubb AT hotmail DOT com> wrote in
news:O9**************@TK2MSFTNGP10.phx.gbl:
I first converted the BitArray to an Int32.

Dim result As Int32 = 0

For i As Int32 = 0 To outputBank.Length - 1
If outputBank(i) Then
result += System.Math.Pow(2, i)
End If
Next

MessageBox.Show(result.ToString("X"))

More info on formatting strings:
http://msdn.microsoft.com/library/de...y/en-us/cpguid
e/html/cpconstandardnumericformatstrings.asp
"Joel Moore" <as*******@asdaadad.com> wrote in message
news:Xn******************************@207.46.248.1 6...
Maybe I'm just easily baffled after an all-nighter but I can't seem
to figure out how to represent a BitArray as a hexadecimal string.

For example:

Dim outputBank As New BitArray(8)

outputBank(0) = True
outputBank(1) = False
outputBank(2) = False
outputBank(3) = True
outputBank(4) = True
outputBank(5) = False
outputBank(6) = True
outputBank(7) = False

The hexadecimal string for the above array of bits would be "59".
How can I obtain that string? Is there an easy built-in method for
doing this or do I have to build something on my own?

Joel Moore



Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by FalkoG | last post: by
1 post views Thread by Marc Lefebvre | last post: by
2 posts views Thread by Joel Moore | last post: by
3 posts views Thread by ShihChengYu | last post: by
7 posts views Thread by elliotng.ee | last post: by
2 posts views Thread by semedao | last post: by
6 posts views Thread by sweeet_addiction16 | last post: by
10 posts views Thread by cmdolcet69 | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.