简体   繁体   English

UTF16字符串为普通文本

[英]UTF16 string to normal text

I have one column in DB containing UTF16 string and I want to convert the UTF16 string into normal text. 我在数据库中有一列包含UTF16字符串,我想将UTF16字符串转换为普通文本。
How to achieve this in c# ? 如何在C#中实现呢?

For example : 例如 :

Source : 0645 0631 062D 0628 0627 0020 0627 0644 0639 0627 0644 0645
Convert : مرحبا العالم

I presume that source is simply a string containing the byte values, as this is one thing not quite clear from your question. 我认为source只是一个包含字节值的字符串,因为从您的问题来看这还不是很清楚。

You first need to turn that into a byte array. 您首先需要将其转换为字节数组。 Of course you first need to remove the blanks. 当然,您首先需要删除空白。

// Initialize the byte array
string sourceNoBlanks = source.Replace(" ", "").Trim();
if ((sourceNoBlanks.Length % 2) > 0)
    throw new ArgumentException("The length of the source string must be a multiple of 2!");

byte[] sourceBytes = new byte[source.Length / 2];

// Then, create the bytes
for (int i = 0; i < sourceBytes.Length; i++)
{
    string byteString = sourceNoBlanks.Substring(i*2, 2);
    sourceBytes[i] = Byte.Parse(byteString, NumberStyles.HexNumber);
}

After that you can easily convert it to string: 之后,您可以轻松地将其转换为字符串:

string result = Encoding.UTF32.GetString(sourceBytes);

I suggest you read up on the UTF32 encoding to understand little/big endian encoding. 我建议您阅读UTF32编码以了解小/大字节序编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM