Converting byte array to string and back again in C# Converting byte array to string and back again in C# arrays arrays

Converting byte array to string and back again in C#


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;}


When you have raw bytes (8-bit possibly-not-printable characters) and want to manipulate them as a .NET string and turn them back into bytes, you can do so by using

Encoding.GetEncoding(1252)

instead of UTF8Encoding. That encoding works to take any 8-bit value and convert it to a .NET 16-bit char, and back again, without losing any information.

In the specific case you describe above, with a binary file, you will not be able to "mess with metadata in the header" and have things work correctly unless the length of the data you mess with is unchanged. For example, if the header contains

{any}{any}ABC{any}{any}

and you want to change ABC to DEF, that should work as you'd like. But if you want to change ABC to WXYZ, you will have to write over the byte that follows "C" or you will (in essence) move everything one byte further to the right. In a typical binary file, that will mess things up greatly.

If the bytes after "ABC" are spaces or null characters, there's a better chance that writing larger replacement data will not cause trouble -- but you still cannot just replace ABC with WXYZ in the .NET string, making it longer -- you would have to replace ABC{whatever_follows_it} with WXYZ. Given that, you might find that it's easier just to leave the data as bytes and write the replacement data one byte at a time.


Due to the fact that .NET strings use Unicode strings, you can no longer do this like people did in C. In most cases, you should not even attempt to go back and forth from string<->byte array unless the contents are actually text.

I have to make this point clear: In .NET, if the byte[] data is not text, then do not attempt to convert it to a string except for the special Base64 encoding for binary data over a text channel. This is a widely-held misunderstanding among people that work in .NET.