ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

ToBytes

Convert a string to a byte array

Source

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

Example

string test="hello world";
btye[] bytes=test.ToBytes();

Author: Daniel Pamich

Submitted on: 25 jul 2014

Language: C#

Type: string

Views: 3007