96 bit binary to hex conversion and vice versa

A

akash deep batra

hi

i want to convert a 96 bit binary number into a hexadecimal number.

e.g binary number= 001100010001010000100101011110111111010101110100010110000101011000101010000000000000000000000000

how can i do that in C#.

also i want to convert a hexadecimal number (24 digits) into a binary
number

e.g
hex number = 3114257BF57458562A000000


thanks and regards
akash deep batra
 
A

Armin Zingler

akash deep batra said:
hi

i want to convert a 96 bit binary number into a hexadecimal
number.

e.g binary number=
0011000100010100001001010111101111110101011101000101100001010110001010100000
00000000000000000000

how can i do that in C#.

also i want to convert a hexadecimal number (24 digits) into a
binary number

e.g
hex number = 3114257BF57458562A000000


Sorry, VB.Net code: ;-)

(quick&dirty without checks)


Private Function BinStringToHexString(ByVal Text As String) As String
Static sb As New System.Text.StringBuilder(24)
sb.Length = 0
For i As Integer = 0 To 3
sb.Append( _
Convert.ToInt32(Text.Substring(i * 24, 24), 2).ToString("X6") _
)
Next
Return sb.ToString
End Function


Private Function HexStringToBinString(ByVal Text As String) As String

Static table As String() = _
{"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", _
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}

Static sb As New System.Text.StringBuilder(96)

sb.Length = 0
'Stop
For i As Integer = 0 To 23
Dim b As Integer
b = Convert.ToInt32(Text.Chars(i))
Select Case b
Case 48 To 57
sb.Append(table(b - 48))
Case Else
sb.Append(table(b - 55))
End Select
Next
Return sb.ToString
End Function


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 

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