Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  129 Posts | 0 Stories | 874 Comments | 677 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

This is something I wanted to do a while ago.. and ended up coding manually (duh!) like this:

string myString = "a test string";
byte
[] myByteArray = new byte[myString.Length];
int i = 0;
foreach(char c in InStr.ToCharArray())
{
  myByteArray [i] = (
byte
)c;
  i++;
}

Then some kind soul (thanks Michael) mentioned the Encoding class <slaps forehead>...

System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] myByteArray = enc.GetBytes("a text string);
string myString = enc.GetString(myByteArray );

Simple eh ;-)

Tim

posted on Monday, June 27, 2005 1:49 PM

Feedback

# re: How to convert a string to a byte array and convert a byte array to a string 6/28/2005 1:07 AM Eric Newton
yeah just fyi, the encoding classes help you keep the byte ordering right ;)

# re: How to convert a string to a byte array and convert a byte array to a string 5/25/2006 9:05 AM Jignesh
i found it useful thanks.

# re: How to convert a string to a byte array and convert a byte array to a string 8/31/2006 9:31 AM Jordi Serres
Thanks for publishing it. It saved me (and others) some time!

# re: How to convert a string to a byte array and convert a byte array to a string 9/6/2006 5:57 AM Naren
its gonna save me


# re: How to convert a string to a byte array and convert a byte array to a string 1/11/2007 6:32 AM Navdeep Kumar
Thanks i am looking for that

# re: How to convert a string to a byte array and convert a byte array to a string 1/31/2007 6:49 AM Stevie
Or if you like, you can do it in two lines instead of three, like this:

byte[] myByteArray = System.Text.Encoding.ASCII.GetBytes("a text string");

string myString = System.Text.Encoding.ASCII.GetString(myByteArray );

# re: How to convert a string to a byte array and convert a byte array to a string 2/21/2007 5:32 PM goelmk
Thanks. I wrote my complete application and was just planning to write my own function for converting byte array to string. Suddenly thought of finding something on Google and see I found it. Thanks a lot. This is small thing but saved me time. Appreciate it.

- Manoj

# re: How to convert a string to a byte array and convert a byte array to a string 4/25/2007 2:01 AM Anjul Krishna Pandey
Thanks,It's very usefull

# re: How to convert a string to a byte array and convert a byte array to a string 5/7/2007 11:11 AM Alexander Ordin
It is fantantic!!! I was looking for that for so long!!!!!
Bravo, fucking american!!!


# re: How to convert a string to a byte array and convert a byte array to a string 5/18/2007 2:30 PM pfarrell
Exactly what I was looking for. Thanks.

@Stevie: While correct that you could reduce to 2 lines in place of 3. Keeping the enc as a separate variable will allow for easier future refactoring (i.e. it could be set a one location in your code base).

# re: How to convert a string to a byte array and convert a byte array to a string 5/19/2007 5:52 PM boyce
cheers dude saved me some hassle, I was fooled into thinking .ToString(); did something. Then came the confusion followed by the straw clutching then came google :)

# Thanz a Lot 8/16/2007 5:19 AM Ashik Iqbal
Thank you very much for this help.

# re: How to convert a string to a byte array and convert a byte array to a string 8/17/2007 9:26 AM poor_beggar
does it also work to convert your string to another encoding?

byte[] myByteArray = System.Text.Encoding.UTF8.GetBytes("a text string");

string myString = System.Text.Encoding.ASCII.GetString(myByteArray );

# re: How to convert a string to a byte array and convert a byte array to a string 10/11/2007 5:32 AM shekhar
very simple and nice! Thank you so much!!!

# re: How to convert a string to a byte array and convert a byte array to a string 10/17/2007 6:45 AM Jayasree
do we need to import any jar for using the System.Text.Encoding??

# re: How to convert a string to a byte array and convert a byte array to a string 12/6/2007 6:32 AM Bilal Haider
Extremly Simple and fully working...! I had been googling for a simple solution since long, Thanx a lot for this.

@pfarrell
i agree with u. 3 line simple code is better that 2 line less readable code ...

# re: How to convert a string to a byte array and convert a byte array to a string 1/24/2008 11:53 PM George
Thanks.

The days when byte ~ char are gone. :)
I was initially frustrated when...

byte[] x = (byte[]) "blahblah"

did not work, until the old head began working.

...Of course, one wonders why every data type is not naturally automatically convertable to byte[]...or may bit[] :)

# re: How to convert a string to a byte array and convert a byte array to a string with out using system objects 2/14/2008 6:23 AM Raj
How to convert a string to a byte array and convert a byte array to a string with out using system objects

# re: How to convert a string to a byte array and convert a byte array to a string 3/25/2008 7:10 AM Agathish
Thanks for the snippet... really appreciate it..

# re: How to convert a string to a byte array and convert a byte array to a string 7/27/2008 1:32 PM MP
String s = "This is test";
byte[] stringtoBytes = s.getBytes();



# re: How to convert a string to a byte array and convert a byte array to a string 8/7/2008 7:02 PM Ganesh
Hi,I was trying to use this code
System.Text.Encoding enc = System.Text.Encoding.ASCII;
However the eclipse editor is saying System.Text cannot be resolved.Any help would be appreciated.

# re: How to convert a string to a byte array and convert a byte array to a string 8/26/2008 12:13 AM Braulio
Nice stuff, but won't work if you are using the special language characters (what the people calls 'Extended ASCII').

Any work arround for the 'Extended ASCII' (using 8 bytes instead of 7).

# re: How to convert a string to a byte array and convert a byte array to a string 10/25/2008 12:52 PM cem
just what I was looking for.. thanks

# re: How to convert a string to a byte array and convert a byte array to a string 11/26/2008 6:22 AM Flint
'Extended ASCII' implementation
public static byte[] GetBytes(string chars)
{

return Array.ConvertAll(chars.ToCharArray(),
delegate(char c)
{
unchecked
{
if (c > 0xFF)
{
return (byte) '?';
}
else
{
return (byte) c;
}

}
}
);
}

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 3 and 7 and type the answer here: