output ANSI encoding for unicode character

N

Nick

Hi,

I am trying to output a string of chinese characters as a
text file. When I open a file for writing from VB, the
file is automatically set to UTF-8 encoding (can tell by
opening the file from notepad). However, when I open
this file from a Chinese program that does not support
unicode, garbage is displayed. So what I have to do is
to first use Notepad to change the encoding of the file
to ANSI encoding, then the file would be displayed
correctly by the Chinese program.

What I would like to do is to directly output the chinese
text string in ANSI format so I don't need to go through
the extra step with notepad.

Could someone please advise?

Thanks!
 
A

Armin Zingler

Nick said:
I am trying to output a string of chinese characters as a
text file. When I open a file for writing from VB, the
file is automatically set to UTF-8 encoding (can tell by
opening the file from notepad). However, when I open
this file from a Chinese program that does not support
unicode, garbage is displayed. So what I have to do is
to first use Notepad to change the encoding of the file
to ANSI encoding, then the file would be displayed
correctly by the Chinese program.

What I would like to do is to directly output the chinese
text string in ANSI format so I don't need to go through
the extra step with notepad.

Could someone please advise?

I'm not sure if this is possible at all. I don't think so because the
unicode encoding can contain much more different characters than ANSI
encoding can. A general answer would be:

Dim fs As IO.FileStream
Dim MyChineseString As String = "<Chinese string>"
Dim sw As IO.StreamWriter

fs = New IO.FileStream( _
"g:\test.txt", IO.FileMode.CreateNew, _
IO.FileAccess.Write, IO.FileShare.Read _
)
sw = New IO.StreamWriter(fs, System.Text.Encoding.Default)
sw.WriteLine(MyChineseString)
sw.Close()
fs.Close()
 
F

Fergus Cooney

Hi Nick,

I'm not sure if this'll help but,

Encoded text files have two bytes at the front which signify the encoding.
If you look at an encoded text file in NotePad, you won't see them. If you use
a hex editor, however, they will show up. If you can discover the two bytes
required for your text, you can fiddle the encoding by outputting these two
bytes first.

Regards,
Fergus
 
C

Cor

Nick,
My gues was that you need to set the culture information to do this, but I
made last times so much missers answering you, that I waited a while.
When you want me to help you with it say it, but I think that with some
thoughts of us you can do it yourself better.

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Hick,
text file. When I open a file for writing from VB, the
file is automatically set to UTF-8 encoding (can tell by
In addition to the others comments, it sounds like you are using the default
encoding when you are opening the file. How are you opening the file for
writing??

Remember that the default encoding for Streams in .NET is UTF-8.

If you specify a Chinese ANSI code page encoding when you open the file, the
file will be encoded as that ANSI code page.

Remember there are actually multiple ANSI code pages that a file could be
encoded to. You can use the System.Text.Encoding.Default encoding to use the
ANSI code page that your system is currently set to.

Imports System.IO
Imports System.Text

Dim writer As New StreamWriter("myfile.txt", False, Encoding.Default)

Or you can use Encoding.GetEncoding to specify a specific (Chinese) ANSI
code page.

Imports System.IO
Imports System.Text

' Chinese Simplified
Dim writer As New StreamWriter("myfile.txt", False,
Encoding.GetEncoding(54936))

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top