c语言 utf 8转字符串,如何将UTF-8字节[]转换为字符串?
我有一個byte[]數(shù)組,它是從一個我所知道的包含UTF-8的文件中加載的。 在一些調(diào)試代碼中,我需要將其轉(zhuǎn)換為字符串。 是否有一個班輪可以做到這一點?
在幕后 ,它應(yīng)該只是一個分配和一個內(nèi)存復(fù)制 ,因此即使未實現(xiàn)它也應(yīng)該是可能的。
#1樓
string result = System.Text.Encoding.UTF8.GetString(byteArray);
#2樓
至少有四種不同的方式可以完成此轉(zhuǎn)換。
編碼的GetString
,但如果原始字節(jié)具有非ASCII字符,則將無法找回原始字節(jié)。
BitConverter.ToString
輸出是一個以“-”分隔的字符串,但是沒有.NET內(nèi)置方法將字符串轉(zhuǎn)換回字節(jié)數(shù)組。
Convert.ToBase64String
您可以使用Convert.FromBase64String輕松將輸出字符串轉(zhuǎn)換回字節(jié)數(shù)組。
注意:輸出字符串可以包含“ +”,“ /”和“ =”。 如果要在URL中使用字符串,則需要對其進行顯式編碼。
HttpServerUtility.UrlTokenEncode
您可以使用HttpServerUtility.UrlTokenDecode輕松地將輸出字符串轉(zhuǎn)換回字節(jié)數(shù)組。 輸出字符串已經(jīng)是URL友好的了! 缺點是,如果您的項目不是Web項目,則需要System.Web程序集。
一個完整的例子:
byte[] bytes = { 130, 200, 234, 23 }; // A byte array contains non-ASCII (or non-readable) characters
string s1 = Encoding.UTF8.GetString(bytes); // ���
byte[] decBytes1 = Encoding.UTF8.GetBytes(s1); // decBytes1.Length == 10 !!
// decBytes1 not same as bytes
// Using UTF-8 or other Encoding object will get similar results
string s2 = BitConverter.ToString(bytes); // 82-C8-EA-17
String[] tempAry = s2.Split('-');
byte[] decBytes2 = new byte[tempAry.Length];
for (int i = 0; i < tempAry.Length; i++)
decBytes2[i] = Convert.ToByte(tempAry[i], 16);
// decBytes2 same as bytes
string s3 = Convert.ToBase64String(bytes); // gsjqFw==
byte[] decByte3 = Convert.FromBase64String(s3);
// decByte3 same as bytes
string s4 = HttpServerUtility.UrlTokenEncode(bytes); // gsjqFw2
byte[] decBytes4 = HttpServerUtility.UrlTokenDecode(s4);
// decBytes4 same as bytes
#3樓
定義:
public static string ConvertByteToString(this byte[] source)
{
return source != null ? System.Text.Encoding.UTF8.GetString(source) : null;
}
使用方法:
string result = input.ConvertByteToString();
#4樓
使用(byte)b.ToString("x2") ,輸出b4b5dfe475e58b67
public static class Ext {
public static string ToHexString(this byte[] hex)
{
if (hex == null) return null;
if (hex.Length == 0) return string.Empty;
var s = new StringBuilder();
foreach (byte b in hex) {
s.Append(b.ToString("x2"));
}
return s.ToString();
}
public static byte[] ToHexBytes(this string hex)
{
if (hex == null) return null;
if (hex.Length == 0) return new byte[0];
int l = hex.Length / 2;
var b = new byte[l];
for (int i = 0; i < l; ++i) {
b[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return b;
}
public static bool EqualsTo(this byte[] bytes, byte[] bytesToCompare)
{
if (bytes == null && bytesToCompare == null) return true; // ?
if (bytes == null || bytesToCompare == null) return false;
if (object.ReferenceEquals(bytes, bytesToCompare)) return true;
if (bytes.Length != bytesToCompare.Length) return false;
for (int i = 0; i < bytes.Length; ++i) {
if (bytes[i] != bytesToCompare[i]) return false;
}
return true;
}
}
#5樓
將byte[]轉(zhuǎn)換為string似乎很簡單,但是任何類型的編碼都可能使輸出字符串混亂。 這個小功能可以正常工作而不會產(chǎn)生任何意外結(jié)果:
private string ToString(byte[] bytes)
{
string response = string.Empty;
foreach (byte b in bytes)
response += (Char)b;
return response;
}
總結(jié)
以上是生活随笔為你收集整理的c语言 utf 8转字符串,如何将UTF-8字节[]转换为字符串?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 医保卡可以当银行卡用吗里面可以存钱吗
- 下一篇: c语言输入整数要求输出字符,求C语言 将