By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

Convert BitArray to hexadecimal string

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.