user2574499
user2574499

Reputation: 1

.txt to String with Encoding

The goal is simple. Grab some French text containing special characters from a .txt file and stick it into the variable "content". All is working well except that all instances of the character "à" are being interpreted as "À". Did I pick the wrong encoding (UTF7) or what?

Any help would be much appreciated.

// Using ecoding to ensure special characters work
Encoding enc = Encoding.UTF7;

// Make sure we get all the information about special chars
byte[] fileBytes = System.IO.File.ReadAllBytes(path);

// Convert the new byte[] into a char[] and then into a string. 
char[] fileChars = new char[enc.GetCharCount(fileBytes, 0, fileBytes.Length)];
enc.GetChars(fileBytes, 0, fileBytes.Length, fileChars, 0);
string fileString = new string(fileChars);

 // Insert the resulting encoded string "fileString" into "content"
 content = fileString;

Upvotes: 0

Views: 105

Answers (1)

usr
usr

Reputation: 171236

Your code is correct besides the wrong encoding. Find the correct one and plug it in. Nobody uses UTF7 so this is probably not it.

Maybe it is a non-Unicode one. Try Encoding.Default. That one empirically often helps in Germany.

Also, just use File.ReadAllText. It does everything you are doing.

Upvotes: 3

Related Questions