我正在写一个winform,将书面文本转换为 Unicode 数字和 UTF8 数字。这个位运作良好
//------------------------------------------------------------------------
// Convert to UTF8
// The return will be either 1 byte, 2 bytes or 3 bytes.
//-----------------------------------------------------------------------
UTF8Encoding utf8 = new UTF8Encoding();
StringBuilder builder = new StringBuilder();
string utext = rchtxbx_text.Text;
// do one char at a time
for (int text_index = 0; text_index < utext.Length; text_index++)
{
byte[] encodedBytes = utf8.GetBytes(utext.Substring(text_index, 1));
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("{0}", Convert.ToString(encodedBytes[index], 16));
}
builder.Append(" ");
}
rchtxtbx_UTF8.SelectionFont = new System.Drawing.Font("San Serif", 20);
rchtxtbx_UTF8.AppendText(builder.ToString() + "\r");
例如,字符乘义ש给我e4b998 e4b989 d7a9,请注意我混合使用了LtoR和RtoL文字。现在,如果用户输入数字 e4b998 ,我想向他们显示它是乘,采用Unicode 4E58
我尝试了一些事情,但距离我最近的是
Encoding utf8 = Encoding.UTF8;
rchtxbx_text.Text = Encoding.ASCII.GetString(utf8.GetBytes(e4b998));
我需要怎么做才能输入 e4b998 并将乘写入文本框?
答案 0 :(得分:1)
类似这样的东西:
source
分成2个字符的块:"e4b998"
-> {"e4", "b9", "98"}
Convert
块变成字节Encode
个字节进入最终字符串实施:
string source = "e4b998";
string result = Encoding.UTF8.GetString(Enumerable
.Range(0, source.Length / 2)
.Select(i => Convert.ToByte(source.Substring(i * 2, 2), 16))
.ToArray());
如果您的int
为source
:
答案 1 :(得分:0)
string s_unicode2 = System.Text.Encoding.UTF8.GetString(utf8.GetBytes(e4b998));