Unicode -- 从code point到UTF16的计算方法
v = 0x64321详细描述:
v′ = v - 0x10000
= 0x54321
= 0101 0100 0011 0010 0001
vh = 0101010000 // higher 10 bits of v′
vl = 1100100001 // lower 10 bits of v′
w1 = 0xD800 // the resulting 1st word is initialized with the high bits
w2 = 0xDC00 // the resulting 2nd word is initialized with the low bits
w1 = w1 | vh
= 1101 1000 0000 0000 |
01 0101 0000
= 1101 1001 0101 0000
= 0xD950
w2 = w2 | vl
= 1101 1100 0000 0000 |
11 0010 0001
= 1101 1111 0010 0001
= 0xDF21
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
{
String cp = tbUnicodeCodePoint.Text.Trim();
{
int n = Convert.ToInt32(cp, 16);
if (n < 0 || n > 0x10FFFF)
{
MessageBox.Show(cp + " is not in 0x0 - 0x10FFFF");
return;
}
if (n < 0x10000)
{
tbUTF16Code.Text = Convert.ToString(n, 16);
return;
}
else
{
n -= 0x10000;
int h = n >> 10;
int l = n & 0x3FF;
h |= 0xD800;
l |= 0xDC00;
tbUTF16Code.Text = Convert.ToString(h, 16) + " " + Convert.ToString(l, 16);
}
}
catch (Exception ex)
{
MessageBox.Show("Invalid text: " + cp + Environment.NewLine + ex.Message);
}
}
}
}本文出自 “GONE WITH THE WIND” 博客,请务必保留此出处http://h2appy.blog.51cto.com/609721/144639