如何将字符串转换为utf 16 [英] How do I convert string into utf 16

查看:255
本文介绍了如何将字符串转换为utf 16的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在asp.net c中转换unicode中的代码#请尽快帮助我。是c#代码是unicode



我尝试过:



什么都没有尝试但我不知道unicode如此不好混淆怎么做谷歌不会给出完整的细节

i dont know how to convert the code in unicode in asp.net c# please help me asap. is c# code is unicode

What I have tried:

nothing to be tried yet im not aware of unicode so ill be confuse how to do that google would not be giving the full detail

推荐答案

.Net使用UTF-16编码。您必须仅在使用其他编码获取数据或传递数据时进行转换。



另请参阅 .NET中的字符编码| Microsoft Docs [ ^



使用文本文件,可以通过字节顺序指示使用的编码标记 - 维基百科 [ ^ ](UTF强制要求 - 16,可选 - 但建议 - 对于UTF-8)和文件用于特定协议(如HTML或XML)时的标题。



对于那里的Web服务器和应用程序通常也是默认编码的配置选项(例如< globalization> Element | Microsoft Docs [ ^ ])。





找到了真正的问题。

.Net uses the UTF-16 encoding. You have to convert only when having data or passing data using other encodings.

See also Character Encoding in .NET | Microsoft Docs[^].

With text files, the used encoding can be indicated by a Byte order mark - Wikipedia[^] (mandatory for UTF-16, optional - but recommended - for UTF-8) and by headers when the file is for a specific protocol like HTML or XML.

For web servers and applications there are also often configuration options for the default encoding (e.g. the <globalization> Element | Microsoft Docs[^]).


The real question has been found.
引用:

我想显示unicode格式的字符串数据,比如我的名字是Shahbaz是字符串格式,将它转换为unicode是

U + 0053 U + 0068 U + 0061 U + 0068 U + 0062 U +0061 U + 007A

i want to display the string data in unicode format like im having my name is "Shahbaz" is in string format and the convert it to the unicode is
U+0053 U+0068 U+0061 U+0068 U+0062 U+0061 U+007A

您只需要通过迭代输入字符串的字符来创建使用该格式的字符串:

You just have to create a string using that format by iterating over the characters of the input string:

// Using a StringBuilder is more efficient than appending to a string
StringBuilder sb = new StringBuilder(input.Length * 7);
// Iterate over the characters of the input string
foreach (char c in input)
{
    // Append the character code
    sb.AppendFormat("U+{0:X4} ", c);
}
// Create final output string
string output = sb.ToString();

[/ EDIT]


这篇关于如何将字符串转换为utf 16的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆