472,364 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

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 4550
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Achim Domma | last post by:
Hi, I read some text from a utf-8 encoded text file like this: text = codecs.open('example.txt','r','utf8').read() If I pass this text to a COM object, I can see that there is still the BOM...
1
by: AstroDrabb | last post by:
I am trying to update some Active Directory attributes in C#. The C# app creates new computer objects and then populates some attributes. However, I am having problems with one attribute named...
7
by: Venus | last post by:
Hello, I am trying to generate a dynamic form at runtime and would like to do it using "<asp: ..." form elements as follows Build up the string that is placed somewhere in the HTML code the...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
4
by: Pere Raphael | last post by:
Hi all, I am writng some code to read AD user properties but I am not having much luck reading octet values. For example, there is a property userPassword which returns System.Byte. When I...
4
by: ad | last post by:
I have a string variable. How can I convert the string to MemoryStream?
7
by: 一首诗 | last post by:
Is there any simple way to solve this problem?
0
by: =?Utf-8?B?dGhsMTAwMA==?= | last post by:
Hi, i need help getting the logonHours from AD for a specific user. I found a vbscript doing exactly what i want: On Error Resume Next Dim arrLogonHoursBytes(20) Dim arrLogonHoursBits(167)...
10
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
15
by: Steve | last post by:
Hi All, I have a registration code that currently has 4 bytes that are unused. I'd like to store a future date in those 4 bytes. Is there a way to convert a date to a 4 byte string? When I...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.