views:

67

answers:

1

I've got a program that in a nutshell reads values from a SQL database and writes them to a tab-delimited text file.

The issue is that some of the values in the database have special characters (TM, dash, ellipsis, etc.) When written to the text file, the formatting is lost and they come across as junk "™ or – etc"

When the value is viewed in the immediate window, before it is written to the txt file, everything looks fine. My guess is that this is an issue of encoding. But, I'm not real sure how to proceed, where to look, or what to look for.

Is this ASCII or UTF-8? If it's one of those how do I correct it before it's written to the text file.

Here's how I build the text file (where feedStr is a StringBuilder)

objReader = New StreamWriter(filePath)
objReader.Write(feedStr)
objReader.Close()
+1  A: 

The default encoding for StreamWriter is UTF8 (with no byte order mark). Your result file is ok, the question is what do you open it in afterwards? If you open it in a UTF8 capable text editor, the characters should look the way you want.

You can also write the text file in another encoding, for example iso-8859-1 (latin1)

objReader = New StreamWriter(filePath, false, Encoding.GetEncoding("iso-8859-1"))
Mikael Svenson
Mikael, you're the man! That worked like a charm! Perfect answer!
s15199d
ALMOST... I changed it to Encoding.GetEncoding("utf-8")That fixed all of my special characters. But, now it mucks with the ControlChars.Tab delimiter.FYI... my output file has to be UTF-8 encoded, b/c its going to Google and that's the required format.
s15199d
You could probably use Encoding.UTF8 instead of GetEncoding.
Mikael Svenson
Mikael, you are correct Encoding.UTF8 does work. But, it's still screwing up my ControlChars.Tab delimiter. I've tried VBTab, Chr(9) to no avail. UTF8 fixes my special chars, but it destroys my tab delimiter. Any suggestions? Thanks in advance!
s15199d
Weird, as Chr(9) is correct, also for UTF8. If you open your result in an hex editor, what characters are you seeing for tab?
Mikael Svenson
Screw it...the problem was the program I was using the view it. I kept viewing it in Excel...which apparently does not like the UTF8 format.When I viewed it in notepad and Firefox, it looks perfect.Mikael...your "the man" status has been restored. Thanks for your help and persistence!
s15199d