Last active
January 1, 2016 23:39
-
-
Save Santarh/8217537 to your computer and use it in GitHub Desktop.
Converting character encoding on C#
This file contains hidden or 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
using System; | |
using System.IO; | |
using System.Text; | |
public class Test | |
{ | |
public static void Main() | |
{ | |
// your code goes here | |
string str_unicode = "あいうえお"; // (あ)0x3042, (い)0x3044 ... | |
var unicode = Encoding.Unicode; | |
var utf8 = Encoding.UTF8; | |
byte[] byte_unicode = unicode.GetBytes(str_unicode); | |
// utf-16LE のため、(あ) は 0x42(66), 0x30(48) | |
Console.WriteLine("Unicode 'あ' の下位バイト: " + byte_unicode[0]); | |
Console.WriteLine("Unicode 'あ' の上位バイト: " + byte_unicode[1]); | |
// Unicode(UTF-16LE)からUTF-8に変換 | |
byte[] byte_utf8 = Encoding.Convert(unicode, utf8, byte_unicode); | |
string string_utf8 = utf8.GetString(byte_utf8); | |
// utf-8 のため、(あ) は 0xe3(227), 0x81(129), 0x82(130) | |
Console.WriteLine("UTF-8 'あ' の第1バイト: " + byte_utf8[0]); | |
Console.WriteLine("UTF-8 'あ' の第2バイト: " + byte_utf8[1]); | |
Console.WriteLine("UTF-8 'あ' の第3バイト: " + byte_utf8[2]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行結果