Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting byte array to string and back again in C#

Tags:

So here's the deal: I'm trying to open a file (from bytes), convert it to a string so I can mess with some metadata in the header, convert it back to bytes, and save it. The problem I'm running into right now is with this code. When I compare the string that's been converted back and forth (but not otherwise modified) to the original byte array, it's unequal. How can I make this work?

public static byte[] StringToByteArray(string str) {     UTF8Encoding encoding = new UTF8Encoding();     return encoding.GetBytes(str); }  public string ByteArrayToString(byte[] input) {     UTF8Encoding enc = new UTF8Encoding();     string str = enc.GetString(input);     return str; } 

Here's how I'm comparing them.

byte[] fileData = GetBinaryData(filesindir[0], Convert.ToInt32(fi.Length)); string fileDataString = ByteArrayToString(fileData); byte[] recapturedBytes = StringToByteArray(fileDataString); Response.Write((fileData == recapturedBytes)); 

I'm sure it's UTF-8, using:

StreamReader sr = new StreamReader(filesindir[0]); Response.Write(sr.CurrentEncoding); 

which returns "System.Text.UTF8Encoding".

like image 378
Brian Hicks Avatar asked Sep 14 '09 15:09

Brian Hicks


People also ask

Can you convert byte into string?

One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable.

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

Can you store a byte array as a string?

For text or character data, we use new String(bytes, StandardCharsets. UTF_8) to convert the byte[] to a String directly. However, for cases that byte[] is holding the binary data like the image or other non-text data, the best practice is to convert the byte[] into a Base64 encoded string.

Are byte arrays immutable?

A bytearray is very similar to a regular python string ( str in python2. x, bytes in python3) but with an important difference, whereas strings are immutable, bytearray s are mutable, a bit like a list of single character strings.


1 Answers

Try the static functions on the Encoding class that provides you with instances of the various encodings. You shouldn't need to instantiate the Encoding just to convert to/from a byte array. How are you comparing the strings in code?

Edit

You're comparing arrays, not strings. They're unequal because they refer to two different arrays; using the == operator will only compare their references, not their values. You'll need to inspect each element of the array in order to determine if they are equivalent.

public bool CompareByteArrays(byte[] lValue, byte[] rValue) {     if(lValue == rValue) return true; // referentially equal     if(lValue == null || rValue == null) return false; // one is null, the other is not     if(lValue.Length != rValue.Length) return false; // different lengths      for(int i = 0; i < lValue.Length; i++)     {         if(lValue[i] != rValue[i]) return false;     }      return true; } 
like image 109
Adam Robinson Avatar answered Sep 22 '22 23:09

Adam Robinson