473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 8875
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 to convert the position after decimal point. I...
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 to Hexadecimal.
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 example: Dim outputBank As New BitArray(8) ...
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 function to enhance my project, but the API only...
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 26 14:03:35 2006...
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 1 give me 0 (zero) when it should give me 2 ! ...
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 generated has to be an unsigned long.
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) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.