C# - Convert String to Byte-Array and back

By , 12/27/2012
(0 ratings)
Two methods to convert a String to a Byte-Array and vice versa.

Author: Jan Welker, translation by Michael List
//it is possible to hand over a char[] too
private byte[] StringToByteArray(string str)
    //use any encoding you want to
    //return System.Text.Encoding.ASCII.GetBytes(str);
    return System.Text.Encoding.Default.GetBytes(str);

private string ByteArrayToString(byte[] arr)
    //use any encoding you want to
    //return System.Text.Encoding.ASCII.GetString(str);
    return System.Text.Encoding.Default.GetString(arr);



Log in, to comment!