-
-
Save NKid/9b88cba8ce6ed6228c32 to your computer and use it in GitHub Desktop.
Encoding.GetEncoding().GetBytes()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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