Friday, February 6, 2009

Converting byte array to string and string to byte array

The following code will convert a string to byte array.

First of all you will have to add the using System.Text; namespace in your application.

string str = "Hello";
byte[] str2byte = Encoding.ASCII.GetBytes(str);

Now the "str2byte" array will contain the byte reprsentation of the worf 'Hello'.

The following code will convert a byte array to string :

byte[] bytearray = "Somevalue";
string str = Encoding.ASCII.GetString(bytearray);

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails