Convert BitArray to hexadecimal string

J

Joel Moore

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
 
J

Joel Moore

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.
 

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