views:

1655

answers:

3

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

edit: 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));

update: yes, I'm sure it's UTF8, using:

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

which returns "System.Text.UTF8Encoding"

+7  A: 

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;
}
Adam Robinson
I've edited the question to show how... the code doesn't show up right in the comment!
Reactor5
I tried this, they return that they're not of the same length. It must be somewhere else.
Reactor5
Take a look at the documentation for the UTF8 encoding. There is an option as to whether or not to specify the preamble. If you're finding that your generated byte array is longer than the original, then that is likely your issue. Again, you need to make sure that UTF8 is, in fact, the right encoding. As to how you can tell, you would have to ask whoever is supplying you with the data.
Adam Robinson
+1  A: 

You problem would appear to be the way your comparing the array of bytes:

Response.Write((fileData == recapturedBytes));

This will always return false since your comparing the address of the byte array not the values it contains. Compare the string data, or use a method of comparing the byte arrays. You could also do this instead:

Response.Write(Convert.ToBase64String(fileData) == Convert.ToBase64String(recapturedBytes));
csharptest.net
+1  A: 

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.

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

280Z28
String<->byte[] conversions should generally be done through one of the System.Text.Encoding classes, not the BitConverter class. BitConverter.ToString converts a byte array into a hexadecimal string representation of the numbers, it does **not** convert a byte array into a string.
Adam Robinson
Heh, I should have removed that line once I knew it wasn't the point of my post.
280Z28