Unicode メモ

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

class Program {
    static string Hex<T>(T b) {
        return string.Format("{0:X}", b);
    }

    static void Main() {
        string s = "𪚲"; // Unicode 2A6B2(D869+DEB2)
                         // UTF8 F0 AA 9A B2
        Console.WriteLine(s);              // 𪚲
        Console.WriteLine(s.Length);       // 2
        Console.WriteLine("\U0002A6B2");   // 𪚲
        Console.WriteLine("\uD869\uDEB2"); // 𪚲

        byte[] b1 = BitConverter.GetBytes(s[0]);
        byte[] b2 = BitConverter.GetBytes(s[1]);
        Console.WriteLine(string.Join(" ", b1.Select(Hex))); // 69 D8
        Console.WriteLine(string.Join(" ", b2.Select(Hex))); // B2 DE

        int hi = (b1[1] << 8) | b1[0];
        int lo = (b2[1] << 8) | b2[0];
        int uni = 0x10000 + (hi - 0xD800) * 0x400 + (lo - 0xDC00);
        Console.WriteLine(Hex(uni)); // 2A6B2

        Console.WriteLine(Hex(char.ConvertToUtf32(s, 0))); // 2A6B2
        Console.WriteLine(char.ConvertFromUtf32(uni));     // 𪚲

        var enc = new UTF8Encoding();
        byte[] bytes = enc.GetBytes(s);
        Console.WriteLine(string.Join(" ", bytes.Select(Hex))); // F0 AA 9A B2
        Console.WriteLine(enc.GetString(bytes)); // 𪚲
    }
}

リンク