Results 1 to 12 of 12

Thread: Saving Chinese CHaracters to txtfile

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Saving Chinese CHaracters to txtfile

    Hi all, i'm trying to save chiense CHaracters to a textfile on my pc, here's what i have.

    Code:
    Private Sub CommandButton1_Click()
            ' create a Unicode text file with Chinese character
            ' Dan1 and English character D.
            Open "unicode.txt" For Binary As #1
            Put #1, , TextBox1.Text
            Close #1
          End Sub
    The chinese characters i'm trying to save are these 3 words "简体字"

    The textbox i'm using are 2.0 Forms and when pasting these 3 words on the textbox it displays properly after setting the font to Arial_Unicode_MS, Script is Chinese_GB2312

    however when i click on the button what i get in my "unicode.txt" is ???

    My font on my notepad for this textfile is also set to Arial_Unicode_MS, script : Chinese_GB2312. If i copy and paste the 3 chinese characters "简体字" it displays properly on my notepad.

    Anyone know what's the code i'm missing to fix this problem?

    thx

  2. #2
    Hyperactive Member CHAMPGARY's Avatar
    Join Date
    Jul 2002
    Posts
    386

    Re: Saving Chinese CHaracters to txtfile

    Hello,

    Did you try the same with Microsoft word document.If your requirement is not only for text file you may try this. I have created 2-3 applications where I need to store/access data with Japanese text. For me forms2 controls & microsoft word document,Access database works fine.

    Regards,
    Champgary

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Saving Chinese CHaracters to txtfile

    Hi, i tried MSWord, it gives me this "???ND "

    When i copy and paste "简体字" on MSWord, it tells me that the font is "玭伐╰参虏砰" which i have no idea what does that mean..

  4. #4
    Hyperactive Member CHAMPGARY's Avatar
    Join Date
    Jul 2002
    Posts
    386

    Re: Saving Chinese CHaracters to txtfile

    Hello Sylpheed ,

    Are you using windows non-chinese version ( I mean english,other language).Because I had developed my application on Japanese windows 2000. So it need not to install any language packs or support but if u r not using then for input of Asian characters like chinese/japanes/korean you should install Microsoft global IME(Input Method Editor). I think it's freeware and very easy to Install.

    Please refer thelink1
    for more information.

    Regards,
    Champgary

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Saving Chinese CHaracters to txtfile

    Hi, thx for the reply.

    Yup i'm using english version of WInXP Pro, but i do have the asian language packs installed. Do i need to activate something? or set some settings even with my language pack installed for it to work?

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Saving Chinese CHaracters to txtfile

    The problem here is that the default file handling functions convert strings automatically to ANSI (and from ANSI to Unicode when loading).

    Now, to skip this problem, you have to skip the automatical conversion by using a byte array (which is the easiest solution in this case):

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim barTemp() As Byte
    3.     barTemp = Text1.Text
    4.     Open "unicode.txt" For Binary As #1
    5.         Put #1, , barTemp
    6.     Close #1
    7. End Sub

    When loading, you reserve enough space to a byte array and then use Get to load all the data to the byte array. Then just do Text1.Text = barTemp and you should be able to load Unicode as well.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Saving Chinese CHaracters to txtfile

    Hi thx for the tip, but i get an error at this line

    barTemp = Text1.Text

    is there some function i need to call to convert string to byte?

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Saving Chinese CHaracters to txtfile

    No, it should work from there. Do you really have a textbox named Text1?

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Saving Chinese CHaracters to txtfile

    Opps.. lol my mistake.. it nolonger gives an error but my txt file says it's "€{SOW["

    same for MSWOrd.

    This also happens when i call StrConv(textbox1.text, vbUnicode)

  10. #10
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Saving Chinese CHaracters to txtfile

    It is perfectly correct, it is the string in Unicode format. When loading, you need to do this:

    VB Code:
    1. Private Sub Command2_Click()
    2.     Dim barTemp() As Byte
    3.     ReDim barTemp(FileLen("unicode.txt") - 1)
    4.     Open "unicode.txt" For Binary As #1
    5.         Get #1, , barTemp
    6.     Close #1
    7.     Text1.Text = barTemp
    8. End Sub

    This should load it correctly.


    Oh, and Word displays it incorrectly because the text file has no BoM (byte order mark). It is two bytes long and it is used to tell the text file is a Unicode file. Although old text editors display it as garbage and you need to skip it in the code if you add it to the file. Without a BoM the file is assumed by programs to be an ANSI file.

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Saving Chinese CHaracters to txtfile

    Thank you very much for your help. I'm very new to using asian languages in VB projects. when you say BoM, is it the first 2 characters? I heard when you save unicodes there's 2 characters said ahead then it's what you want to save.

    As for MSWord, do you mean i can save my chinese characters on MSword and it will show itself as chinese characters when i open the word doc?

    How do i go about skipping the BoM when saving to file?

  12. #12
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Saving Chinese CHaracters to txtfile

    When you save a regular textfile (which is all different from Microsoft Word document), you first write the BoM (which is two bytes in this case, you can find more information about it on unicode.org's FAQ). When you open your regular text file in Word, it checks for BoM and if it finds it, it will treat the file as the BoM tells it. So you have to save the BoM if you want other programs to treat the text file correctly.

    Basically the BoM is as long as one character (2 bytes = one character in UTF-16). So when you're saving, you could just add the BoM character at the beginning, push out the Unicode text data and you're done. When loading, you drop the first character, it is as simple as doing Mid$(CStr(barTemp), 2). It will be up to you if you want to make the program wise enough to notice if the BoM actually exists and that it is correct.

    Before you ask what is Big Endian and Little Endian: Windows uses Little Endian
    Last edited by Merri; Jan 16th, 2006 at 09:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width