469,332 Members | 6,874 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

How to convert string "AD-A6-0D-1F" to byte[]?

Hi, All,
We can use BitConverter.ToString(byte[]) to a string,
but how to get the byte[] from a string like "AD-A6-0D-1F"?
Nov 16 '05 #1
4 4396
Hello,
For ASCII:
System.Text.Encoding.ASCII.GetBytes(string); //returns byte array.
For Unicode:
System.Text.Encoding.Unicode.GetBytes(string); //returns bytes array
(little-endian byte format)

Cheers :)

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
See the following code:

Byte[] bs={0xAD,0xA6,0x0D,0x1F};
string str=BitConverter.ToString(bs) ; //str="AD-A6-0D-1F"
byte[] bs2=System.Text.Encoding.ASCII.GetBytes(str);
//bs2[]={0x41,0x44,0x2D,0x41,0x36,0x2D,0x30,0x44,0x2D,0x3 1,0x46}

bs2!=bs
I think that required to write a routing like:

//------------------------------------------------
public static byte[] ByteConvert(string s)
{
byte[] rt=new byte[(s.Length+1)/3];
for(int j=0,i=0;i<s.Length;i+=3)
{
rt[j]=Convert.ToByte(s.Substring(i,2),16);// .ToByte(s);
j++;
}
return rt;
}

//------------------------------------------------

"Maqsood Ahmed" <ma***********@gawab.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hello,
For ASCII:
System.Text.Encoding.ASCII.GetBytes(string); //returns byte array.
For Unicode:
System.Text.Encoding.Unicode.GetBytes(string); //returns bytes array
(little-endian byte format)

Cheers :)

Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
Maqsood Ahmed <ma***********@gawab.com> wrote:
For ASCII:
System.Text.Encoding.ASCII.GetBytes(string); //returns byte array.
For Unicode:
System.Text.Encoding.Unicode.GetBytes(string); //returns bytes array
(little-endian byte format)

Cheers :)


Those will certainly convert strings to bytes, but not in the way that
the OP wants.

However, you don't need to get the substring of the string for each
pair of hex digits. Here's some code I wrote a while ago to parse hex
strings. It *doesn't* take account of the '-' between each pair of hex
digits, but could easily be modified to do so.

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 16 '05 #4
or you can try Int32.Parse( string, NumberStyles.HexNumber ), then convert
int to byte[]. that too however doesn't support the '-', so you have to
strip them out.

"Jon Skeet [C# MVP]" wrote:
Maqsood Ahmed <ma***********@gawab.com> wrote:
For ASCII:
System.Text.Encoding.ASCII.GetBytes(string); //returns byte array.
For Unicode:
System.Text.Encoding.Unicode.GetBytes(string); //returns bytes array
(little-endian byte format)

Cheers :)


Those will certainly convert strings to bytes, but not in the way that
the OP wants.

However, you don't need to get the substring of the string for each
pair of hex digits. Here's some code I wrote a while ago to parse hex
strings. It *doesn't* take account of the '-' between each pair of hex
digits, but could easily be modified to do so.

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 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Achim Domma | last post: by
1 post views Thread by AstroDrabb | last post: by
4 posts views Thread by Pere Raphael | last post: by
7 posts views Thread by 一首诗 | last post: by
reply views Thread by =?Utf-8?B?dGhsMTAwMA==?= | last post: by
10 posts views Thread by CuTe_Engineer | last post: by
15 posts views Thread by Steve | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.