ファイルを (バイトから) 開き、それを文字列に変換して、ヘッダーのメタデータをいじり、バイトに戻して保存しようとしています。私が今直面している問題は、このコードにあります。元のバイト配列と前後に変換された (変更されていない) 文字列を比較すると、等しくありません。どうすればこれを機能させることができますか?
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;
}
これが私がそれらを比較している方法です。
byte[] fileData = GetBinaryData(filesindir[0], Convert.ToInt32(fi.Length));
string fileDataString = ByteArrayToString(fileData);
byte[] recapturedBytes = StringToByteArray(fileDataString);
Response.Write((fileData == recapturedBytes));
私はそれがUTF-8であると確信しています:
StreamReader sr = new StreamReader(filesindir[0]);
Response.Write(sr.CurrentEncoding);
「System.Text.UTF8Encoding」を返します。