1

I am writing an application that must generate a plain text file with fixed-sized columns.

My current code is:

Dim MyFilePath As String = Path & FILE_PREFIX & FileNr & ".TXT"

IO.File.Delete(MyFilePath)

Dim FileStr As New IO.StreamWriter(MyFilePath, False, <ENCODER HERE>)
Do While r.Read
    FileStr.WriteLine(r("TXTLine"))
Loop
FileStr.Close()
r.Close()

My problem is that I have some special characters like: "ñ", "à", etc., and I can't find the right encoding.

  • If I use the default, then it replaces "ñ" with two characters.
  • If I use ASCII then all special characters end up as: "?"
  • If I use UTF-8 then all text is OK, but it adds a "ÿ" in the first byte of the file.

I need the special characters to be written in the textfile just as they came in the datareader. And I can't have extra characters added because columns are of fixed lenght...

What could I do?

1

3 Answers 3

4

How are you checking that you got the right encoding?

If you're simply opening the file up in Notepad, Windows-1252 is probably the encoding you want.

Encoding.GetEncoding("Windows-1252")

will give you that.

Note that I notice that you say that some columns are fixed length. Are you writing data to a file for import by another tool? If so, then you should check what encoding that other tool requires, not which tool happens to look pretty in whatever other tool you might use to look at the file with (like Notepad).

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

2 Comments

The file will be read by a third party application, to which i have no access. I am looking at the file with V.exe (an HEX Viewer), and i just need it to be compatible with this: asciitable.com. That means that "Ñ" equals Dec: 164 or HEX: F1
"If you're simply opening the file in notepad, Windows-1252 is probably the encoding you want". True if you are in the US or Western Europe, not necessarily true in the rest of the world. Of course those folks tend to be more knowledgable about encodings, so it is sensible advice to hand out.
1

The problem is not that you can't find the right encoding, the problem is you don't know what your SOURCE encoding is. Plain ASCII only really has 256 characters and if your source contains accented characters that aren't in the ASCII palette, you won't be able to write them in the ASCII encoding. End of story.

What you need to be doing is re-examining the communication between the two systems. If you need to have unicode characters, then both the source and destination will need to accept unicode be it in a flat file or in some XML document.

You cannot put a square peg (unicode accented characters) into a round hole (ASCII file).

1 Comment

You can as long as the area of the square is equal to the area of the circle multiplied by 2/pi. ;)
1

Wait, you say that the columns must be of "fixed length". That suggests you are trying to have this file read by another application - are you even sure this second application can read "special" (Unicode) characters?

Are you sure that the other application is not expecting ASCII only, and simply has a specific code page that handles a few of those special characters using totally different numbers (interprets high bit ASCII characters in some local language)?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.