-2

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

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

3

First, you need to know what the encoding is for your file. Then, you can just use the System.Text.Encoding class to conveniently convert the byte array to a string.

For instance, if your file is in UTF-8, you can simply do:

string s = System.Text.Encoding.UTF8.GetString(bytes);

If your encoding is different, just pick a different property from the Encoding class, but the pattern is the same.

EDIT: Short explanation as to why OP's code did not work

The code in your original post tries to interpret the byte array as if it was already in the same encoding as the char type, which is UTF-16. So, unless your file happens to use UTF-16 encoding, it simply won't work. Using the Encoding class is the way to go.

sstan
  • 35,425
  • 6
  • 48
  • 66
  • it's windows-1252 or what it called Westren – Mohammad Al Baghdadi Dec 12 '15 at 03:59
  • Then you can use `System.Text.Encoding.GetEncoding("windows-1252").GetString(bytes)`, or, if `windows-1252` is your default OS encoding, you can simply use `System.Text.Encoding.Default.GetString(bytes)` – sstan Dec 12 '15 at 04:03
  • I don't think OP looking for code that actually perform correct conversion ("solution posted like 1000 times"), but rather in fixing errors in code provided. Otherwise it should simply be duplicate. – Alexei Levenkov Dec 12 '15 at 04:13
  • @Alexei: His code can't be fixed because it's designed to work with UTF-16 only. The fix is to use the `Encoding` class. Wouldn't you agree? – sstan Dec 12 '15 at 04:20
  • If "use `Encoding`" is the answer you should have simply VTC as duplicate of standard http://stackoverflow.com/questions/1003275/how-to-convert-byte-to-string. I have no idea why OP asked fr fixing code they provided, but *to me* this question in not about correct implementation but rather why give node does not work (I completely agree that `Encoding` is the way to go, so I'm not planning to remove my downvote on question/answer). – Alexei Levenkov Dec 12 '15 at 04:23
  • 1
    @Alexei: Agreed. I marked as duplicate. And I did edit my answer to make it clear that his code is UTF-16 specific. I think OP really needs to understand that. – sstan Dec 12 '15 at 04:33
-1

why don't you try with default encoding see below snippet

var strString = System.Text.Encoding.Default.GetString(File.Readallbytes(myfile));
koolprasad2003
  • 299
  • 3
  • 23
-1

so i solve the problem with this code it gives me error in ByteArrayToString because the bytes.length is odd so what i do is to check if the bytes.length is even it do the code normally but when it's odd it add one byte as {0} to the end of bytes to it will be even

here is my code:

if (bytes.Length % 2 != 0) { 
                    byte[] newArray = new byte[bytes.Length + 1];
                    bytes.CopyTo(newArray, 1);
                    newArray[0] = byte.Parse("0");
                    bytes= newArray;
                }
  • 1
    Yes, you will avoid the exception this way, but if the file is not UTF-16, you will get a string with corrupted data. – sstan Dec 12 '15 at 04:31