Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read file into byte array is different to string

Tags:

arrays

string

c#

I have a file in visual studio with the following contents:"{"Name":"Pete"}" If I read the file with the following code it appears to create a string with the original value:

byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
string jsonResponse = System.Text.Encoding.UTF8.GetString(byteArray);

However, the string is actually different to the version that exists if I use the following code:

string jsonResponse = "{\"Name\":\"Pete\"}";

Why? (The reason I think it is different is because when I pass each version to a json deserializer it behaves differently)

Thanks.

like image 288
Journeyman Avatar asked Apr 19 '11 11:04

Journeyman


People also ask

Is byte array same as String?

Since bytes is the binary data while String is character data. It is important to know the original encoding of the text from which the byte array has created. When we use a different character encoding, we do not get the original string back.

How do you turn a String into a byte?

We can use String class getBytes() method to encode the string into a sequence of bytes using the platform's default charset. This method is overloaded and we can also pass Charset as argument.

Is used to read from byte array?

The read(byte[ ], int, int) method of ByteArrayInputStream class in Java is used to read the given number of bytes into the given byte array from the ByteArrayOutputStream. This method is different from the above read() method as it can read several bytes at a time.

Is byte array and binary same?

Byte arrays mostly contain binary data such as an image. If the byte array that you are trying to convert to String contains binary data, then none of the text encodings (UTF_8 etc.) will work.

How to read file to byte array in Java?

Java Read File to Byte Array 1. Files.readAllBytes () – Java 8. Files.readAllBytes () is best method if you are using Java 7, 8 and above. 2. FileInputStream – Java 6. Use java.io.FileInputStream for reading the content of a file in Java 6. 3. FileUtils, IOUtils – Apache Commons IO. Another good ...

What is the difference between a byte array and string?

Since bytes is the binary data while String is character data. It is important to know the original encoding of the text from which the byte array has created. When we use a different character encoding, we do not get the original string back. Suppose, we have to read byte array from a file which is encoded in " ISO_8859_1 ".

How to convert a large file into a single-byte array?

So you cannot convert a large file into a single-byte array, though you can read large data using input stream, you need to process them in chunks or using multiple byte arrays. If you like this article and want to learn more about improved file IO in recent Java version, please check the following tutorials:

Which is the best method to read all bytes from a file?

Files.readAllBytes () – Java 8 Files.readAllBytes () is best method if you are using Java 7, 8 and above. 2. FileInputStream – Java 6


2 Answers

Given your final comment in the question, I suspect the problem is that you've got a byte-order mark at the start of the file. Try loading the file like this instead:

string jsonResponse = File.ReadAllText(filePath);

I believe that will strip the BOM for you. Alternatively, you could try explicitly trimming it yourself:

jsonResponse = jsonResponse.TrimStart('\feff');
like image 81
Jon Skeet Avatar answered Oct 18 '22 01:10

Jon Skeet


My guess would be that you have a terminating newline in your file.

You can easily verify if two strings have the same content in C# by just comparing them with a == b.

Here's a short code sample that might help you identify the problem. The strings are output surrounded by < >, which should help you identify surrounding whitespace (which, by the way, can be removed using String.Trim).

byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
string fromFile = System.Text.Encoding.UTF8.GetString(byteArray);
string fromString = "{\"Name\":\"Pete\"}";

if (fromFile == fromString) {
    Console.WriteLine("Strings are the same.");
} else {
    Console.WriteLine("Strings are different!");
    Console.WriteLine("fromFile:   <" + fromFile + ">");
    Console.WriteLine("fromString: <" + fromString + ">");
}
like image 44
Heinzi Avatar answered Oct 18 '22 00:10

Heinzi