tags:

views:

476

answers:

2

I'm trying to display ascii-art in a textbox. If I open a specific .nfo file in notepad with the font "Lucida Console", 9pt, regular, it looks like this :

http://i48.tinypic.com/24zvvnr.png

In my app I set the font of the textbox to "Lucida Console", 9 pt, regular, it looks like this :

http://i49.tinypic.com/2ihq8h0.png

What am I doing wrong ? (Or - what should I do to get it to look like in notepad ?)

+2  A: 

You're probably reading the file with the wrong encoding.

How are you opening the file?

SLaks
using System.IO.StreamReader file = new System.IO.StreamReader(inFileName);and then just ReadLine until the end.Should I specify an encoding somewhere ?
Led
Open the file in VS, click File, Advanced Save Options, and check what encoding it actually is.
SLaks
Then I see what encoding this file is in, but is there a general encoding I can use for all ascii-art files..?
Led
It depends on the files. I have no idea where your files are coming from.
SLaks
+6  A: 

Your problem can be summed up like this: ASCII is not UTF-8, and UTF-8 is not ASCII.

The StreamReader(string) constructor initializes the StreamReader to use Encoding.UTF8, which is a UTF-8 decoder that silently attempts to resolve invalid characters. A very quick glance at the Wikipedia page for .nfo files reveals that most .nfo files are generally encoded in Extended ASCII (aka Code Page 437). While the first 127 characters of ASCII map to the first 127 bit patterns of UTF-8, the encodings are not the same, and so you will get incorrect characters if you use one where the other is expected.

You probably want:

System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(437);
System.IO.StreamReader file = new System.IO.StreamReader(fileName, encoding);
Daniel Pryden
You, Sir, are a hero !Works like a charm, and now I understand the importance of using the right encoding :)Thanks a lot !
Led
No problem, glad I could help.
Daniel Pryden