Unicode-to-string conversion in C#

前端 未结 7 934
你的背包
你的背包 2020-12-20 17:22

How can I convert a Unicode value to its equivalent string?

For example, I have \"రమెశ్\", and I need a function that accepts this Unicode value and returns a string

相关标签:
7条回答
  • 2020-12-20 18:23

    Try the following:

    byte[] bytes = ...;
    
    string convertedUtf8 = Encoding.UTF8.GetString(bytes);
    string convertedUtf16 = Encoding.Unicode.GetString(bytes); // For UTF-16
    

    The other way around is using `GetBytes():

    byte[] bytesUtf8 = Encoding.UTF8.GetBytes(convertedUtf8);
    byte[] bytesUtf16 = Encoding.Unicode.GetBytes(convertedUtf16);
    

    In the Encoding class, there are more variants if you need them.

    0 讨论(0)
提交回复
热议问题