How can I safely convert a byte array into a string and back?
Solution 1
The absolute safest way to convert bytes to a string and back is to use 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.
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.
Solution 2
You can just use the Convert
class as below.
/// <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);
}
Solution 3
You can use Convert.ToBase64 documentation http://msdn.microsoft.com/en-us/library/dhx0d524.aspx
Lemon
Software Developer, Geek, HSP, SDA, ..., open, honest, careful, perfectionist, ... Currently into indoor rowing and rock climbing, just to mention something non-computer-related... Not the best at bragging about myself... so... not sure what more to write... 🤔
Updated on July 08, 2022Comments
-
Lemon almost 2 years
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?
-
Sam Harwell almost 15 yearsThis post should replace the above as the answer. :)
-
Lemon almost 15 yearsDone. Didn't see the note at first. Very important! Thanks, once again Jon Skeet =)
-
UnkwnTech over 12 yearsOnce again Mr. Skeet answers one my questions, and once again he did it before I even asked it. :)
-
But I'm Not A Wrapper Class over 10 yearsThis is a very important post. I have had huge problems with conversion, but solves everything string/byte array related.
-
Roman Ratskey about 10 years@JonSkeet, i am using Convert.To/From Base64 in my code but when the input contains some chars like '/' it simply throws an exception! ? any ideas ?
-
Jon Skeet about 10 years@RuneS: I suggest you ask a new question with a short but complete program demonstrating the problem.