如何安全地将字节数组转换为字符串并返回?(复制)

时间:2021-09-23 06:52:29

This question already has an answer here:

这个问题已经有了答案:

I don't really care about encoding and stuff, as long as I get back the exact same byte array.

我并不关心编码之类的东西,只要我得到的是完全相同的字节数组。

So to sum up: How do I convert a byte array into a string, and then that string back into the same byte array I started with?

综上所述:如何将一个字节数组转换成一个字符串,然后将这个字符串转换回我开始使用的那个字节数组?

3 个解决方案

#1


129  

The absolute safest way to convert bytes to a string and back is to use base64:

将字节转换成字符串并返回的最安全的方法是使用base64:

string base64 = Convert.ToBase64String(bytes);
byte[] bytes = Convert.FromBase64String(base64);

That way you're guaranteed not to get "invalid" unicode sequences such as the first half of a surrogate pair without the second half. Nothing's going to decide to normalize the data into something strange (it's all ASCII). There's no chance of using code points which aren't registered in Unicode, or anything like that. Oh, and you can cut and paste without much fear, too.

这样,您就保证不会得到“无效”的unicode序列,比如没有第二个字符的代理对的前半部分。没有什么会决定要将数据规范化成某种奇怪的东西(它都是ASCII)。不可能使用未在Unicode中注册的代码点,或者类似的东西。哦,你也可以毫不畏惧地剪切和粘贴。

Yes, you end up with 4 characters for every 3 bytes - but that's a small price to pay for the knowledge that your data won't be corrupted.

是的,每3个字节就有4个字符——但是要知道你的数据不会被破坏,这是一个很小的代价。

#2


3  

You can just use the Convert class as below.

您可以使用Convert类,如下所示。

/// <summary>
/// Converts a string to byte array
/// </summary>
/// <param name="input">The string</param>
/// <returns>The byte array</returns>
public static byte[] ConvertToByteArray(string input)
{
    return input.Select(Convert.ToByte).ToArray();
}

/// <summary>
/// Converts a byte array to a string
/// </summary>
/// <param name="bytes">the byte array</param>
/// <returns>The string</returns>
public static string ConvertToString(byte[] bytes)
{
    return new string(bytes.Select(Convert.ToChar).ToArray());
}

/// <summary>
/// Converts a byte array to a string
/// </summary>
/// <param name="bytes">the byte array</param>
/// <returns>The string</returns>
public static string ConvertToBase64String(byte[] bytes)
{
    return Convert.ToBase64String(bytes);
}

#3


2  

You can use Convert.ToBase64 documentation http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

您可以使用转换。ToBase64 http://msdn.microsoft.com/en-us/library/dhx0d524.aspx文档

#1


129  

The absolute safest way to convert bytes to a string and back is to use base64:

将字节转换成字符串并返回的最安全的方法是使用base64:

string base64 = Convert.ToBase64String(bytes);
byte[] bytes = Convert.FromBase64String(base64);

That way you're guaranteed not to get "invalid" unicode sequences such as the first half of a surrogate pair without the second half. Nothing's going to decide to normalize the data into something strange (it's all ASCII). There's no chance of using code points which aren't registered in Unicode, or anything like that. Oh, and you can cut and paste without much fear, too.

这样,您就保证不会得到“无效”的unicode序列,比如没有第二个字符的代理对的前半部分。没有什么会决定要将数据规范化成某种奇怪的东西(它都是ASCII)。不可能使用未在Unicode中注册的代码点,或者类似的东西。哦,你也可以毫不畏惧地剪切和粘贴。

Yes, you end up with 4 characters for every 3 bytes - but that's a small price to pay for the knowledge that your data won't be corrupted.

是的,每3个字节就有4个字符——但是要知道你的数据不会被破坏,这是一个很小的代价。

#2


3  

You can just use the Convert class as below.

您可以使用Convert类,如下所示。

/// <summary>
/// Converts a string to byte array
/// </summary>
/// <param name="input">The string</param>
/// <returns>The byte array</returns>
public static byte[] ConvertToByteArray(string input)
{
    return input.Select(Convert.ToByte).ToArray();
}

/// <summary>
/// Converts a byte array to a string
/// </summary>
/// <param name="bytes">the byte array</param>
/// <returns>The string</returns>
public static string ConvertToString(byte[] bytes)
{
    return new string(bytes.Select(Convert.ToChar).ToArray());
}

/// <summary>
/// Converts a byte array to a string
/// </summary>
/// <param name="bytes">the byte array</param>
/// <returns>The string</returns>
public static string ConvertToBase64String(byte[] bytes)
{
    return Convert.ToBase64String(bytes);
}

#3


2  

You can use Convert.ToBase64 documentation http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

您可以使用转换。ToBase64 http://msdn.microsoft.com/en-us/library/dhx0d524.aspx文档