统一code到字符串转换在C# [英] Unicode-to-string conversion in C#

查看:83
本文介绍了统一code到字符串转换在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能统一code值转换为其对应的字符串?

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

例如,我有రమెశ్,我需要接受这种统一code值,并返回一个字符串的函数。

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

我一直在寻找在System.Text.Encoding.Convert()函数,但是,这并不需要在一个统一code值;它需要两个编码和字节数组。

I was looking at the System.Text.Encoding.Convert() function, but that does not take in a Unicode value; it takes two encodings and a byte array.

我bascially有我需要保存在一个字符串字段,然后回来后和字符串转换先回一个字节数组的字节数组。

I bascially have a byte array that I need to save in a string field and then come back later and convert the string first back to a byte array.

所以我用ByteConverter.GetString(字节阵列)的字节数组保存到一个字符串,但我不能让它回到一个字节数组。

So I use ByteConverter.GetString(byteArray) to save the byte array to a string, but I can't get it back to a byte array.

推荐答案

请尝试以下操作:

byte[] bytes = ...;

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

周围使用'的GetBytes()的另一种方式:

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.

这篇关于统一code到字符串转换在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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