我有一个字节[]数组,从一个文件加载,我碰巧知道包含UTF-8。
在一些调试代码中,我需要将其转换为字符串。是否有一个单行程序可以做到这一点?
在表面之下,它应该只是一个分配和一个memcopy,所以即使没有实现,也应该是可能的。
我有一个字节[]数组,从一个文件加载,我碰巧知道包含UTF-8。
在一些调试代码中,我需要将其转换为字符串。是否有一个单行程序可以做到这一点?
在表面之下,它应该只是一个分配和一个memcopy,所以即使没有实现,也应该是可能的。
当前回答
string result = System.Text.Encoding.UTF8.GetString(byteArray);
其他回答
当你不知道编码时,从字节数组转换到字符串的一般解决方案:
static string BytesToStringConverted(byte[] bytes)
{
using (var stream = new MemoryStream(bytes))
{
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
}
试试这个控制台应用程序:
static void Main(string[] args)
{
//Encoding _UTF8 = Encoding.UTF8;
string[] _mainString = { "Hello, World!" };
Console.WriteLine("Main String: " + _mainString);
// Convert a string to UTF-8 bytes.
byte[] _utf8Bytes = Encoding.UTF8.GetBytes(_mainString[0]);
// Convert UTF-8 bytes to a string.
string _stringuUnicode = Encoding.UTF8.GetString(_utf8Bytes);
Console.WriteLine("String Unicode: " + _stringuUnicode);
}
据我所知,没有一个给出的答案保证正确的行为与空终止。直到有人告诉我不同的,我写了自己的静态类处理以下方法:
// Mimics the functionality of strlen() in c/c++
// Needed because niether StringBuilder or Encoding.*.GetString() handle \0 well
static int StringLength(byte[] buffer, int startIndex = 0)
{
int strlen = 0;
while
(
(startIndex + strlen + 1) < buffer.Length // Make sure incrementing won't break any bounds
&& buffer[startIndex + strlen] != 0 // The typical null terimation check
)
{
++strlen;
}
return strlen;
}
// This is messy, but I haven't found a built-in way in c# that guarentees null termination
public static string ParseBytes(byte[] buffer, out int strlen, int startIndex = 0)
{
strlen = StringLength(buffer, startIndex);
byte[] c_str = new byte[strlen];
Array.Copy(buffer, startIndex, c_str, 0, strlen);
return Encoding.UTF8.GetString(c_str);
}
使用startIndex的原因是在我正在处理的示例中,我需要将byte[]解析为一个以null结尾的字符串数组。在简单的情况下,可以安全地忽略它
string result = ASCIIEncoding.UTF8.GetString(byteArray);
至少有四种不同的转换方式。
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