Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created March 3, 2014 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisMcKee/9336278 to your computer and use it in GitHub Desktop.
Save ChrisMcKee/9336278 to your computer and use it in GitHub Desktop.
String to Byte Array and Reverse
public static class StringHelper
{
public static byte[] ToByteArray(this string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static string GetString(this byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment