-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

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

it's windows-1252 or what it called Westren
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)
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: 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?
@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.
|
-1

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

var strString = System.Text.Encoding.Default.GetString(File.Readallbytes(myfile));

3 Comments

Why OP should "try" to do that? And how your suggestion is different from other answer?
because i dont know what kind of files i'm going to choose maybe unicode or utf-8 or what ever
@MohemmadAlBughdadi please make sure to read joelonsoftware.com/articles/Unicode.html
-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 Comment

Yes, you will avoid the exception this way, but if the file is not UTF-16, you will get a string with corrupted data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.