Skip to content

Instantly share code, notes, and snippets.

@NKid
Created January 18, 2013 09:02
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 NKid/9b88cba8ce6ed6228c32 to your computer and use it in GitHub Desktop.
Save NKid/9b88cba8ce6ed6228c32 to your computer and use it in GitHub Desktop.
Encoding.GetEncoding().GetBytes()
GetByteCode("狂", "Ascii"); //狂 -> Ascii -> 3F
GetByteCode("狂", "Big5"); //狂 -> Big5 -> A8 67
GetByteCode("狂", "UTF-8"); //狂 -> UTF-8 -> E7 8B 82
byte[] b = new byte[] { 0xA8, 0x67 };
GetEncode(b, "Ascii"); // A8 67 -> Ascii -> ?g
GetEncode(b, "Big5"); // A8 67 -> Big5 -> 狂
private static void GetEncode(byte[] b, string code)
{
string str = Encoding.GetEncoding(code).GetString(b);
Console.WriteLine("{1} -> {0} -> {2}", code, PrintByteAry(b), str);
}
private static void GetByteCode(string str, string code)
{
byte[] b = Encoding.GetEncoding(code).GetBytes(str);
Console.WriteLine("{1} -> {0} -> {2}", code, str, PrintByteAry(b));
}
private static string PrintByteAry(byte[] b)
{
string tmp = string.Empty;
for (int i = 0; i < b.Length; i++)
{
tmp = tmp + " " + b[i].ToString("X2");
}
return tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment