473,498 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4687
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
28235
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
7650
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
3559
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
13383
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
3659
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
61079
by: ad | last post by:
I have a string variable. How can I convert the string to MemoryStream?
7
49821
by: 一首诗 | last post by:
Is there any simple way to solve this problem?
0
1488
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
2082
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
4328
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...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.