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

  • Thread starter Thread starter Ping
  • Start date Start date
P

Ping

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"?
 
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
 
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,0x31,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 said:
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 = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));

}
return new string (result);
}
 
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 said:
Maqsood Ahmed said:
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 = (char)(ParseHexDigit(hex[hexIndex++])*16+
ParseHexDigit(hex[hexIndex++]));

}
return new string (result);
}
 
Back
Top