我有一个字节[]数组,从一个文件加载,我碰巧知道包含UTF-8。
在一些调试代码中,我需要将其转换为字符串。是否有一个单行程序可以做到这一点?
在表面之下,它应该只是一个分配和一个memcopy,所以即使没有实现,也应该是可能的。
我有一个字节[]数组,从一个文件加载,我碰巧知道包含UTF-8。
在一些调试代码中,我需要将其转换为字符串。是否有一个单行程序可以做到这一点?
在表面之下,它应该只是一个分配和一个memcopy,所以即使没有实现,也应该是可能的。
当前回答
至少有四种不同的转换方式。
Encoding's GetString, but you won't be able to get the original bytes back if those bytes have non-ASCII characters. BitConverter.ToString The output is a "-" delimited string, but there's no .NET built-in method to convert the string back to byte array. Convert.ToBase64String You can easily convert the output string back to byte array by using Convert.FromBase64String. Note: The output string could contain '+', '/' and '='. If you want to use the string in a URL, you need to explicitly encode it. HttpServerUtility.UrlTokenEncodeYou can easily convert the output string back to byte array by using HttpServerUtility.UrlTokenDecode. The output string is already URL friendly! The downside is it needs System.Web assembly if your project is not a web project.
完整的例子:
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
其他回答
定义:
public static string ConvertByteToString(this byte[] source)
{
return source != null ? System.Text.Encoding.UTF8.GetString(source) : null;
}
使用:
string result = input.ConvertByteToString();
除了选择的答案,如果你使用。net 3.5或。net 3.5 CE,你必须指定解码的第一个字节的索引,以及解码的字节数:
string result = System.Text.Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
我在这篇文章中看到了一些答案,这可能被认为是完整的基础知识,因为我在c#编程中有几种方法来解决相同的问题。唯一需要考虑的是纯UTF-8和带有BOM的UTF-8之间的区别。
Last week, at my job, I needed to develop one functionality that outputs CSV files with a BOM and other CSV files with pure UTF-8 (without a BOM). Each CSV file encoding type will be consumed by different non-standardized APIs. One API reads UTF-8 with a BOM and the other API reads without a BOM. I needed to research the references about this concept, reading the "What's the difference between UTF-8 and UTF-8 without BOM?" Stack Overflow question, and the Wikipedia article "Byte order mark" to build my approach.
最后,我的c#编程的UTF-8编码类型(BOM和纯)需要类似于下面的例子:
// For UTF-8 with BOM, equals shared by Zanoni (at top)
string result = System.Text.Encoding.UTF8.GetString(byteArray);
//for Pure UTF-8 (without B.O.M.)
string result = (new UTF8Encoding(false)).GetString(byteArray);
BitConverter类可用于将字节[]转换为字符串。
var convertedString = BitConverter.ToString(byteAttay);
BitConverter类的文档可以在MSDN上打印。
至少有四种不同的转换方式。
Encoding's GetString, but you won't be able to get the original bytes back if those bytes have non-ASCII characters. BitConverter.ToString The output is a "-" delimited string, but there's no .NET built-in method to convert the string back to byte array. Convert.ToBase64String You can easily convert the output string back to byte array by using Convert.FromBase64String. Note: The output string could contain '+', '/' and '='. If you want to use the string in a URL, you need to explicitly encode it. HttpServerUtility.UrlTokenEncodeYou can easily convert the output string back to byte array by using HttpServerUtility.UrlTokenDecode. The output string is already URL friendly! The downside is it needs System.Web assembly if your project is not a web project.
完整的例子:
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