473,979 Members | 9,528 Online

Hello, Newsgroupians:

I've a question regarding Hexadecimal to binary conversion.

Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).

EXA:

string str = "FA38";
string binStr = SomeClass.Hex2B in(str);

This would produce the following output...

"11111010001110 00"
Thank you,
Trecius
Jun 27 '08 #1
11 7725
Trecius <Tr*****@discus sions.microsoft .comwrote:
I've a question regarding Hexadecimal to binary conversion.

Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).

EXA:

string str = "FA38";
string binStr = SomeClass.Hex2B in(str);

This would produce the following output...

"11111010001110 00"
Given that there are only 16 hex digits, and each hex digit maps to
exactly 4 characters, it's relatively straightforward . There are no
doubt ways to do it with shorter code than this, but this way is pretty
simple:
using System;
using System.Text;

class Test
{
static void Main()
{
string binStr = HexToBin("FA38" );
Console.WriteLi ne(binStr);
}

{"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"};
static string HexToBin(string input)
{
StringBuilder builder = new StringBuilder(i nput.Length*4);
foreach (char c in input)
{
if (c >= '0' && c <='9')
{
builder.Append( Nybbles[c-'0']);
}
else if (c >= 'a' && c <= 'f')
{
builder.Append( Nybbles[c-'a'+10]);
}
else if (c >= 'A' && c <= 'F')
{
builder.Append( Nybbles[c-'A'+10]);
}
else
{
throw new FormatException ("Invalid hex digit: "+c);
}
}
return builder.ToStrin g();
}
}

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
Trecius wrote:
I've a question regarding Hexadecimal to binary conversion.

Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).

EXA:

string str = "FA38";
string binStr = SomeClass.Hex2B in(str);

This would produce the following output...

"11111010001110 00"
There are 16 hexadecimal digits, corresponding to 16 4-bit patterns. So the
idiot's way of doing it (also called "the simplest thing that could possibly
work") would be

private static readonly string[] binarySequences = new string[] {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};

static int hexDigitToInt(c har hexDigit) {
return hexDigit >= '0' && hexDigit <= '9' ? hexDigit - '0' : hexDigit
- 'A' + 10;
}
static string Hex2Bin(string hexDigits) {
StringBuilder result = new StringBuilder(h exDigits.Length * 4);
foreach (char digit in hexDigits) {
result.Append(b inarySequences[hexDigitToInt(d igit)]);
}
return result.ToString ();
}

And despite being the idiot's way, this is actually not bad. (Once you put
in validation, that is.)
--
J.
Jun 27 '08 #3
Trecius wrote:
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).

EXA:

string str = "FA38";
string binStr = SomeClass.Hex2B in(str);

This would produce the following output...

"11111010001110 00"
C# 2.0 / .NET 2.0:

public static string Hex2Bin20(strin g s)
{
StringBuilder sb = new StringBuilder() ;
foreach(char c in s.ToCharArray() )
{

sb.Append(Conve rt.ToString(Con vert.ToInt32(c. ToString(), 16),
}
return sb.ToString();
}

C# 3.0 / .NET 3.5:

public static string Hex2Bin35(strin g s)
{
return String.Join("", (from c in s.ToCharArray() select
Convert.ToStrin g(Convert.ToInt 32(c.ToString() , 16), 2).PadLeft(4,
'0')).ToArray() );
}

Arne

PS: I think I would use the 2.0 version on 3.5 as well - it is
Jun 28 '08 #4
On Jun 27, 6:56*pm, Trecius <Trec...@discus sions.microsoft .comwrote:
Hello, Newsgroupians:

I've a question regarding Hexadecimal to binary conversion.

Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. *How can I accomplish this with minimal code? *I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).

EXA:

string str = "FA38";
string binStr = SomeClass.Hex2B in(str);

This would produce the following output...

"11111010001110 00"

Thank you,

Trecius

int i = Convert.ToInt32 ("FA38", 16);

Console.WriteLi ne(Convert.ToSt ring(i, 2));
Jun 28 '08 #5
On Jun 27, 10:18*pm, parez <psaw...@gmail. comwrote:
On Jun 27, 6:56*pm, Trecius <Trec...@discus sions.microsoft .comwrote:
Hello, Newsgroupians:
I've a question regarding Hexadecimal to binary conversion.
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. *How can I accomplish this with minimal code? *I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).
EXA:
string str = "FA38";
string binStr = SomeClass.Hex2B in(str);
This would produce the following output...
"11111010001110 00"
Thank you,
Trecius

* * * * * * int i = Convert.ToInt32 ("FA38", 16);

* * * * * * Console.WriteLi ne(Convert.ToSt ring(i, 2));
Jun 28 '08 #6
parez wrote:
On Jun 27, 6:56 pm, Trecius <Trec...@discus sions.microsoft .comwrote:
>I've a question regarding Hexadecimal to binary conversion.

Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).

EXA:

string str = "FA38";
string binStr = SomeClass.Hex2B in(str);

This would produce the following output...

"1111101000111 000"

int i = Convert.ToInt32 ("FA38", 16);

Console.WriteLi ne(Convert.ToSt ring(i, 2));
"I've seen other posts, but they are restricted to the size of the
hexadecimal string, for they use Convert.ToStrin g(...)"
?

Arne
Jun 28 '08 #7
On Jun 27, 10:20*pm, parez <psaw...@gmail. comwrote:
On Jun 27, 10:18*pm, parez <psaw...@gmail. comwrote:
On Jun 27, 6:56*pm, Trecius <Trec...@discus sions.microsoft .comwrote:
Hello, Newsgroupians:
I've a question regarding Hexadecimal to binary conversion.
Suppose I have a long hexadecimal string, and I would like to convertit to
a binary string. *How can I accomplish this with minimal code? *I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).
EXA:
string str = "FA38";
string binStr = SomeClass.Hex2B in(str);
This would produce the following output...
"11111010001110 00"
Thank you,
Trecius
* * * * * * int i = Convert.ToInt32 ("FA38", 16);
* * * * * * Console.WriteLi ne(Convert.ToSt ring(i, 2));

but you could do it for each character in the string and append the
output of convert.tostrin g.
Jun 28 '08 #8
On Jun 27, 10:21*pm, Arne Vajhøj <a...@vajhoej.d kwrote:
parez wrote:
On Jun 27, 6:56 pm, Trecius <Trec...@discus sions.microsoft .comwrote:
I've a question regarding Hexadecimal to binary conversion.
Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. *How can I accomplish this with minimal code? *I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).
EXA:
string str = "FA38";
string binStr = SomeClass.Hex2B in(str);
This would produce the following output...
"11111010001110 00"
* * * * * * int i = Convert.ToInt32 ("FA38", 16);
* * * * * * Console.WriteLi ne(Convert.ToSt ring(i, 2));

* *"I've seen other posts, but they are restricted to the size of the
* *hexadecimal string, for they use Convert.ToStrin g(...)"
?

Arne
hehe.. I didnt :)
Jun 28 '08 #9
Trecius wrote:
Hello, Newsgroupians:

I've a question regarding Hexadecimal to binary conversion.

Suppose I have a long hexadecimal string, and I would like to convert it to
a binary string. How can I accomplish this with minimal code? I've seen
other posts, but they are restricted to the size of the hexadecimal string,
for they use Convert.ToStrin g(...).

EXA:

string str = "FA38";
string binStr = SomeClass.Hex2B in(str);

This would produce the following output...

"11111010001110 00"
Thank you,
Trecius
Hi there,

string ToBin(string str)
{
string hex;
string bin;
int num;

bin = string.Empty;
hex = str;
num = int.Parse(hex, System.Globaliz ation.NumberSty les.HexNumber);

while (num 0)
{
bin = string.Format(" {0}{1}", (num & 1) == 1 ? "1" : "0", bin);
num >>= 1;
}

return bin;
}

I did a little test, it seems to be working fine.

Regards.
Jun 28 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.