472,110 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

output ANSI encoding for unicode character

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!
Nov 20 '05 #1
4 18805
"Nick" <yk****@usc.edu> schrieb
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()


--
Armin

Nov 20 '05 #2
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
Nov 20 '05 #3
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
Nov 20 '05 #4
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

"Nick" <yk****@usc.edu> wrote in message
news:26****************************@phx.gbl... 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!

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Michael | last post: by
4 posts views Thread by Richard | last post: by
1 post views Thread by Ma³y Piotruœ | last post: by
10 posts views Thread by Mark Rae | last post: by
4 posts views Thread by Antimon | last post: by
9 posts views Thread by emagzz | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.