By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

Convert from string to byte[]

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Yama <Ya**@discussions.microsoft.com> wrote:
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[i] = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));
}
return new string (result);
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
Hi Yama,

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

Cheers,
Steve Goodyear
Vancouver, Canada
Nov 17 '05 #3

P: n/a
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[i]) << 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));
}
}

Nov 17 '05 #4

P: n/a
That does not work. I get an exception telling me Invalid length for a
Base-64 string.

"Steve Goodyear" wrote:
Hi Yama,

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

Cheers,
Steve Goodyear
Vancouver, Canada

Nov 17 '05 #5

P: n/a
Thanks to all my problem has been resolved with your help!

"Bruce Wood" wrote:
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[i]) << 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));
}
}

Nov 17 '05 #6

P: n/a
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';

Nov 17 '05 #7

P: n/a
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;

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.