473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Similar topics

4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
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...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
5
by: da1978 | last post by:
Hi experts, I need to convert a string or a Byte array to a string byte array. Its relatively easy to convert a string to an char array or a byte array but not a STRING byte array. i.e. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.