469,601 Members | 2,329 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

String to byte array

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
Nov 15 '05 #1
11 30177
Dan C <MSN/da********@msn.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
"Dan C" <MSN/da********@msn.com> wrote
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.ToCh arArray(),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[i];
tmpBytStr[i] = myByte.ToString();
}

Hope this is what you were looking for :)

--
Brian L. Gorman
Nov 15 '05 #3
Brian L. Gorman <bl******@hotmail.com> wrote:
//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.ToCh arArray(),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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Brian L. Gorman <bl******@hotmail.com> wrote:
//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.ToCh arArray(),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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #5
Horatiu Ripa <un****@businessco.us> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
:) 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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Horatiu Ripa <un****@businessco.us> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #7
Horatiu Ripa <un****@businessco.us> wrote:
:) 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #8
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.

--
Horatiu Ripa
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Horatiu Ripa <un****@businessco.us> wrote:
:) 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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #9
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Brian L. Gorman <bl******@hotmail.com> wrote:
//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.ToCh arArray(),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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #10
Dan C <MSN/da********@msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dan C <MSN/da********@msn.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Simon Schaap | last post: by
6 posts views Thread by Ricardo Quintanilla | last post: by
4 posts views Thread by ThunderMusic | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.