I want to read a file using File.Readallbytes(myfile)
and to convert it to String like
string s=ByteArraytoString(File.Readallbytes(myfile));
but it doesn't really works for every file i choose, instead when the file is unicode it works file otherwise it doesn't ,so if any one can help me in this
public static string ByteArrayToString(byte[] bytes)
{
char[] chars = new char[(bytes.Length / sizeof(char))];
Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
public static byte[] StringToByteArray(string s)
{
byte[] bytes = new byte[s.Length * sizeof(char)];
Buffer.BlockCopy(s.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
so the exception is: in ByteArrayToString method
System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)
i know this soloution posted like 1000 time but no one fix this problem in this code