472,353 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 8712
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: FalkoG | last post by:
Hello colleague I want to convert a floating number for example 5236.9856982 to a hexadecimal number. I'd tried several things but my problem is...
1
by: Marc Lefebvre | last post by:
How can I convert an Int to a BitArray ? Or How can I format an Int to a binairy string ? Thank's Marc Lefebvre
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer...
2
by: Joel Moore | last post by:
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...
3
by: ShihChengYu | last post by:
Dear all: How to convert color image to bi-level image? I have confronted one problem when I build my OCR project. I used an software API...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec...
2
by: semedao | last post by:
Hi , I try the BitArray class to make Xor on 2 byte arrays. The result was that when I Xor 1with 2 I get 3 which is correct then Xoring 3 with...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200)...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.