String to byte array

D

Dan C

Is there a routine in c# that will transform a string
ie"Hello Mom" into a
Byte[] array. I have found
char[] cTmp = pString.ToCharArray();

But I have not been able to figure out how to convert a char into a hex
value and place that value into the byte[].

Thanks for your help

Dan C
 
J

Jon Skeet [C# MVP]

Dan C said:
Is there a routine in c# that will transform a string
ie"Hello Mom" into a
Byte[] array. I have found
char[] cTmp = pString.ToCharArray();

But I have not been able to figure out how to convert a char into a hex
value and place that value into the byte[].

Don't forget that a char is 16 bits, so you'd need two bytes to
represent a char in the simple way.

Normally converting a string to a byte array is done using an Encoding
though.

See http://www.pobox.com/~skeet/csharp/unicode.html
 
B

Brian L. Gorman

Dan C said:
Is there a routine in c# that will transform a string
ie"Hello Mom" into a
Byte[] array. I have found
char[] cTmp = pString.ToCharArray();

But I have not been able to figure out how to convert a char into a hex
value and place that value into the byte[].


Here is a routine I used to convert a string called mPassword to a byte
array:


//Set your string here:
string mPassword = "Hello Mom";

//create the byte[] array here:
byte[] Bytstr = new byte[mPassword.Length];

//convert the string to a byte array here:
System.Text.Encoding.ASCII.GetBytes(mPassword.ToCharArray(),0,mPassword.Leng
th,Bytstr,0);

//for debugging, read the values of each byte from the string in the array:
byte myByte;
string[] tmpBytStr = new string[mPassword.Length];
for(int i = 0; i<mPassword.Length;i++)
{
myByte = Bytstr;
tmpBytStr = myByte.ToString();
}

Hope this is what you were looking for :)
 
J

Jon Skeet [C# MVP]

Brian L. Gorman said:
//Set your string here:
string mPassword = "Hello Mom";

//create the byte[] array here:
byte[] Bytstr = new byte[mPassword.Length];

//convert the string to a byte array here:
System.Text.Encoding.ASCII.GetBytes(mPassword.ToCharArray(),0,mPassword.Leng
th,Bytstr,0);

That's a very long-winded way of doing it. Why not just use:

byte[] Bytstr = Encoding.ASCII.GetBytes (mPassword);

? It does the same thing, but rather more simply (and readably).
 
H

Horatiu Ripa

Or use unmanaged code and map the byte array over the char array. I think
that's fastest.
{
char[] charArray = {'c','b','a'};

byte* pByteArray;

fixed(char* pCharArray = charArray)

{

pByteArray = (byte*)(pCharArray);

}
}
--
you'll have pByteArray pointing to the byte array.
Horatiu Ripa

Jon Skeet said:
Brian L. Gorman said:
//Set your string here:
string mPassword = "Hello Mom";

//create the byte[] array here:
byte[] Bytstr = new byte[mPassword.Length];

//convert the string to a byte array here:
System.Text.Encoding.ASCII.GetBytes(mPassword.ToCharArray(),0,mPassword.Leng
th,Bytstr,0);

That's a very long-winded way of doing it. Why not just use:

byte[] Bytstr = Encoding.ASCII.GetBytes (mPassword);

? It does the same thing, but rather more simply (and readably).
 
J

Jon Skeet [C# MVP]

Horatiu Ripa said:
Or use unmanaged code and map the byte array over the char array. I think
that's fastest.
{
char[] charArray = {'c','b','a'};

byte* pByteArray;

fixed(char* pCharArray = charArray)

{

pByteArray = (byte*)(pCharArray);

}
}

Quick correction: that's not unmanaged code, it's unsafe code. It also
doesn't really convert it to a byte array - you could use it within the
unsafe method, but it's not like you can use it elsewhere. It also
assumes a UTF-16 encoding, which may not be what's wanted.

I'd stick with safe code and Encoding.GetBytes, myself.
 
H

Horatiu Ripa

:) unsafe, of course, the rush... Agree, it cannot be used outside unsafe
scope and assumes UTF-16 but is way much faster on very large volumes of
data. When you have to map a structure over the content of a binary file
that contains something like "records" = structure and "fields" = structure
members , considering that there's no type transformations you have to made,
and you want to consume in high speed the data, or to produce data through a
structure in binary files this approach is way faster (I have a specific
implementation where I've used it and the results were that it worked more
than 10 times faster).
Of course within the limitations you've underlined.
But in usual applications I prefer also avoiding unsafe (and unmanaged :)
too) code. By the way, I would surely like to see the structures having a
ToByteArray and a FromByteArray implemented...

--
Horatiu Ripa

Jon Skeet said:
Horatiu Ripa said:
Or use unmanaged code and map the byte array over the char array. I think
that's fastest.
{
char[] charArray = {'c','b','a'};

byte* pByteArray;

fixed(char* pCharArray = charArray)

{

pByteArray = (byte*)(pCharArray);

}
}

Quick correction: that's not unmanaged code, it's unsafe code. It also
doesn't really convert it to a byte array - you could use it within the
unsafe method, but it's not like you can use it elsewhere. It also
assumes a UTF-16 encoding, which may not be what's wanted.

I'd stick with safe code and Encoding.GetBytes, myself.
 
J

Jon Skeet [C# MVP]

Horatiu Ripa said:
:) unsafe, of course, the rush... Agree, it cannot be used outside unsafe
scope and assumes UTF-16 but is way much faster on very large volumes of
data. When you have to map a structure over the content of a binary file
that contains something like "records" = structure and "fields" = structure
members , considering that there's no type transformations you have to made,
and you want to consume in high speed the data, or to produce data through a
structure in binary files this approach is way faster (I have a specific
implementation where I've used it and the results were that it worked more
than 10 times faster).

Did you try using the other alternative of Buffer.BlockCopy? That would
involve making a copy of the data, but should still be very fast, and
wouldn't involve unsafe code.
 
H

Horatiu Ripa

Hmmm...
I don't think that I can fully use that in my specific case but I have to
take a good thought about that.
Thanks for the hint.
 
D

Dan C

This is almost what I need. Unfortunately instead of "Hello" as decimal 72,
101, 108, 108, 111 I need the values in hex. this would be 48, 65, 6C, 6C,
6F.
I think that these values will still fit in the byte array.

Thanks.

Jon Skeet said:
Brian L. Gorman said:
//Set your string here:
string mPassword = "Hello Mom";

//create the byte[] array here:
byte[] Bytstr = new byte[mPassword.Length];

//convert the string to a byte array here:
System.Text.Encoding.ASCII.GetBytes(mPassword.ToCharArray(),0,mPassword.Leng
th,Bytstr,0);

That's a very long-winded way of doing it. Why not just use:

byte[] Bytstr = Encoding.ASCII.GetBytes (mPassword);

? It does the same thing, but rather more simply (and readably).
 
J

Jon Skeet [C# MVP]

Dan C said:
This is almost what I need. Unfortunately instead of "Hello" as decimal 72,
101, 108, 108, 111 I need the values in hex. this would be 48, 65, 6C, 6C,
6F.
I think that these values will still fit in the byte array.

Byte arrays aren't *in* decimal or hex particularly - they're just
numbers. There's no difference between (say) decimal 16 and hex 10. If
you need to *display* the hex values, then just change how you're
formatting each byte - it's nothing to do with what byte is being
formatted though.
 
D

Dan C

Thanks that was what I needed to know. One last thing is there a better way
to overlay MasterByte[11] to MasterByte[20] with a NewByte array of 10
items?

I currently am using a loop
int nOffset = current posisition in MasterByte array
for(int i = 0; i < newArray.GetUpperBound(0); i++)
{
MasterByte [nOffset++] = NewByte[x];
}

Thanks again
Dan C
 
J

Jon Skeet [C# MVP]

Dan C said:
Thanks that was what I needed to know. One last thing is there a better way
to overlay MasterByte[11] to MasterByte[20] with a NewByte array of 10
items?

I currently am using a loop
int nOffset = current posisition in MasterByte array
for(int i = 0; i < newArray.GetUpperBound(0); i++)
{
MasterByte [nOffset++] = NewByte[x];
}

Have a look at Array.Copy - I think it does what you want it to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top