Convert from string to byte[]

G

Guest

Hi,

I have the following binary data:

StringData = 800006000000;

which is equivalent to the following:

byte[] bytes = new byte[6];
bytes[0] = 0x80;
bytes[1] = 0x00;
bytes[2] = 0x06;
bytes[3] = 0x00;
bytes[4] = 0x00;
bytes[5] = 0x00;

How can I parse the string StringData to the equivalent byte array above?

I tried the following:
byte[] bytes = Encoding.ASCII.GetBytes(StringData);

but it gave me back garbage data.

This bytes[0] = 0x80; is equivalent to bytes[0] = 128;

Any help would be appreciated.

Thanks,

Yama
 
J

Jon Skeet [C# MVP]

Yama said:
I have the following binary data:

StringData = 800006000000;

which is equivalent to the following:

byte[] bytes = new byte[6];
bytes[0] = 0x80;
bytes[1] = 0x00;
bytes[2] = 0x06;
bytes[3] = 0x00;
bytes[4] = 0x00;
bytes[5] = 0x00;

How can I parse the string StringData to the equivalent byte array above?

I tried the following:
byte[] bytes = Encoding.ASCII.GetBytes(StringData);

but it gave me back garbage data.

No, it gave you back exactly what you asked for - but that's not what
you wanted.

I suggest you call byte.Parse repeatedly with 2-character substrings,
specifying NumberStyles.AllowHexSpecifier.

Alternatively, you could use something like this:

static int ParseHexDigit(char c)
{
if (c >= '0' && c <= '9')
{
return c-'0';
}
if (c >= 'a' && c <= 'f')
{
return c-'a'+10;
}
if (c >= 'A' && c <= 'F')
{
return c-'A'+10;
}
throw new ArgumentException ("Invalid hex character");
}

public static string ParseHex(string hex)
{
char[] result = new char[hex.Length/2];

int hexIndex=0;
for (int i=0; i < result.Length; i++)
{
result = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));
}
return new string (result);
}
 
G

Guest

Hi Yama,

You can convert from a string to a byte array using the
Convert.FromBase64String method.

Cheers,
Steve Goodyear
Vancouver, Canada
 
B

Bruce Wood

Your problem description still isn't 100% clear, but here's what I'm
assuming.

You have a string of digits (and possibly letters A-F or a-f) that
represent hexadecimal nibble values:

string StringData = "800006000000";

You want to translate this into a byte array, where each character from
the string becomes half of a byte (a nibble).

I don't think that there are any built-in classes in .NET to do this
for you. You'll have to do it yourself:

byte[] bytes = new byte[StringData.Length / 2];
for (int i = 0; i < StringData.Length - 1; i += 2)
{
int j = i / 2;
byte[j] = (ToNibble(StringData) << 4) + ToNibble(StringData[i +
1];
}

private byte ToNibble(char c)
{
if ('0' <= c && c <= '9')
{
return c - '0';
}
else if ('a' <= c && c <= 'f')
{
return c - 'f';
}
else if ('A' <= c && c <= 'F')
{
return c - 'F';
}
else
{
throw new ArgumentException(String.Format("Character '{0}'
cannot be translated to a hexadecimal value because it is not one of
0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,A,B,C,D,E,F", c));
}
}
 
G

Guest

That does not work. I get an exception telling me Invalid length for a
Base-64 string.
 
B

Bruce Wood

Bugs in my code sample:

The line

return c - 'f';

should read

return c - 'a';

The line

return c - 'F';

should read

return c - 'A';
 
B

Bruce Wood

Bugs in my posted code. The line

return c - 'f';

should read

return c - 'a' + 10;

The line

return c - 'F'

should read

return c - 'A' + 10;
 

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