2016-01-13

Convert a string to byte-array (C#)

You might occasionally need to convert string to byte-array for further manipulation. .NET is providing encoding functions out-of-the-box in System.Text.Encoding namespace:

byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString);
byte[] b2 = System.Text.Encoding.ASCII.GetBytes (myString);

string pi = "\u03a0";
byte[] ascii = System.Text.Encoding.ASCII.GetBytes (pi);
byte[] utf8 = System.Text.Encoding.UTF8.GetBytes (pi);

Console.WriteLine (ascii.Length); //Will print 1
Console.WriteLine (utf8.Length); //Will print 2
Console.WriteLine (System.Text.Encoding.ASCII.GetString (ascii)); //Will print '?'

Remember that internally .NET is using UTF-16 encoding to represent strings and if you want exact bytes, user System.Text.Encoding.Unicode.GetBytes(...)method.
But what if the bytes do not need to be interpreted? Then you don't need to worry about encoding.
Below are plain and dirty function for encoding and decoding string to bytes. If you know what you have encoded you know what to expect to be decoded.

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

static string GetString(byte[] bytes)
{
    char[] chars = new char[bytes.Length / sizeof(char)];
    System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
    return new string(chars);
}

No comments:

Post a Comment