• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 33787
  • Last Modified:

Write File in UTF-8 Format

I am using createTextfile to save file as a unicode file, with the name "index.asp". However, IIS5 only support ANSI, and UTF-8 format ASP file.

How can I save the file as UTF-8 Format?
0
anguslai
Asked:
anguslai
  • 6
  • 3
  • 2
  • +6
1 Solution
 
rspahitzCommented:
Have your tried StrConv(text1.text, vbUnicode)?
0
 
anguslaiAuthor Commented:
P.S. I am Using VB6, Not .NET. Please provide solution for VB6. I need to write ASP file which contain Asian Language (Chinese, Japanese, etc)
0
 
anguslaiAuthor Commented:
rspahitz, the problem is not String Convertion. Now the problem is File Format.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
lmanapureCommented:
Hi,

I've tried this code in VB6 (Service Pack 4)

    Dim fs As New Scripting.FileSystemObject
    Dim a As Scripting.TextStream

    Set a = fs.CreateTextFile("c:\testfile.asp", True)
    a.WriteLine ("<%@Language=VBScript%>")
    a.WriteLine ("<%")
    a.WriteLine ("Response.Write(""This is a test."")")
    a.WriteLine ("%>")
    a.Close
    Set fs = Nothing

It creates a file testfile.asp.
I pasted this file in my wwwroot folder and accessed it via browser.
It worked perfectly OK.

Please specify whether you are getting an error when you try to access the file or not.

Lokesh
0
 
anguslaiAuthor Commented:
Imanapure, You are not saving save unicode.
and, you should also try to put some asian (Chinese, Japanese Character in "This is a test" string.

The following is usage of CreateTextFile:
object.CreateTextFile(filename[, overwrite[, unicode]])
0
 
lmanapureCommented:
Hi,

Please refer to the syntax of CreateTextFile

Syntax

object.CreateTextFile(filename[, overwrite[, unicode]])

In the documentation it is written that
unicode Optional. Boolean value that indicates whether the
        file is created as a Unicode or ASCII file.
        The value is True if the file is created as a
        Unicode file;
        False if it's created as an ASCII file. If
        omitted, an ASCII file is assumed.

So, if you refer my code
Set a = fs.CreateTextFile("c:\testfile.asp", True)

According to the documentation, it is creating a unicode file.
0
 
anguslaiAuthor Commented:
imanapure's code: Set a = fs.CreateTextFile("c:\testfile.asp", True) would work most of the time. However some asian character (0.01%?) in Chinese, Korean and Thai will create error. (I am not sure the other language, but I guess all have same problem) Imanapure's code will hit the wall sooner or later.

The fix is specify it as Unicode: a = fs.CreateTextFile("c:\testfile.asp", True, True). However, ASP can not be save as Unicode file, ASP support only ANSI, and UTF-8 Format.

If your type, and save ASP file as UTF-8 in Notepad, it run prefectly.

I really need a way to save file as UTF-8 in VB. Anyone expert can help?
0
 
anguslaiAuthor Commented:
imanapure's code: Set a = fs.CreateTextFile("c:\testfile.asp", True) would work most of the time. However some asian character (0.01%?) in Chinese, Korean and Thai will create error. (I am not sure the other language, but I guess all have same problem) Imanapure's code will hit the wall sooner or later.

The fix is specify it as Unicode: a = fs.CreateTextFile("c:\testfile.asp", True, True). However, ASP can not be save as Unicode file, ASP support only ANSI, and UTF-8 Format.

If your type, and save ASP file as UTF-8 in Notepad, it run prefectly.

I really need a way to save file as UTF-8 in VB. Anyone expert can help?
0
 
rspahitzCommented:
OK...maybe I'm missing something, but StrConv(text1.text, vbUnicode) will create a Unicode string which can then be written as a Unicode file:

Open "UnicodeFile.txt" for output as #1
print #1,StrConv(text1.text, vbUnicode);
close #1
0
 
anguslaiAuthor Commented:
Hi all,

Actually  I need to genrate ASP file with UTF-8 Format (IIS5 ASP only can use ANSI, or UTF8 File).

I tried StrConv(text1.text, vbUnicode) but what I need is File in UTF8.

Finally I create a new DLL (in Java) to handle WriteUTF function, and call that function from VB.

I Know VB.Net can do UTF8 very Easy (By default, file is UTF8), But VB6 seems not ready for that. (at least, not that straight forward)


0
 
rspahitzCommented:
So what did you get with 'StrConv(text1.text, vbUnicode)'?  UTF-somethingelse?
0
 
MoondancerCommented:
You have 18 OPEN questions here today, all of which need your attention.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder, Mindphaser or I will return to finalize these as soon as possible if after 7 days no response by the Asker.  EXPERTS-->  please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20267711.html
http://www.experts-exchange.com/questions/Q.20268229.html
http://www.experts-exchange.com/questions/Q.20268776.html
http://www.experts-exchange.com/questions/Q.20269463.html
http://www.experts-exchange.com/questions/Q.20270295.html
http://www.experts-exchange.com/questions/Q.20273650.html
http://www.experts-exchange.com/questions/Q.20273639.html
http://www.experts-exchange.com/questions/Q.20274476.html
http://www.experts-exchange.com/questions/Q.20276089.html
http://www.experts-exchange.com/questions/Q.20277202.html
http://www.experts-exchange.com/questions/Q.20277158.html
http://www.experts-exchange.com/questions/Q.20277672.html
http://www.experts-exchange.com/questions/Q.20279544.html
http://www.experts-exchange.com/questions/Q.20280415.html
http://www.experts-exchange.com/questions/Q.20280323.html
http://www.experts-exchange.com/questions/Q.20280324.html
http://www.experts-exchange.com/questions/Q.20291948.html
http://www.experts-exchange.com/questions/Q.20296726.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
------> POINTS FOR EXPERTS awaiting comments are listed in the links below
http://www.experts-exchange.com/commspt/Q.20277028.html (Part 1)
http://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=20295853 (Part 2)
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 
Asta CuCommented:
Zero response, recommend that points go to rspahitz if by the time Moderators finalizes this, no added comment.
Thanks,
":0) Asta
0
 
moduloCommented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0
 
digitrioCommented:
StrConv function make Unicode(UTF-16),
But it's totally diffrent with UTF-8.

in my opinion,
if you really want UTF-8,
you shuld give action once more with API 'WideCharToMultiByte'


0
 
briankejserCommented:
What a mess!

Comments:

1. The old visual basic file commands shouldn't be used. If I remember correctly, Microsoft didn't even provide documentation for those command in VB6. They want you to use FSO or ADODB

2. Unicode is a collection of character sets (e.g. UTF-16, UTF-8). Each encoding will produce a different file (i.e. size and contents)

3. StrConv(strText, vbUnicode) produces UTF-16 (the default Windows Unicode format) not UTF-8

4. Set objTextStream = objFSO.CreateTextFile(strFile, , True) outputs as UTF-16 not UTF-8

There's only one way to do what your asking. Use a stream.

For example ...

Private Sub CreateFile(ByVal pstrFile As String, ByVal pstrData As String)

  Dim objStream As Object
 
  'Create the stream
  Set objStream = CreateObject("ADODB.Stream")

  'Initialize the stream
  objStream.Open

  'Reset the position and indicate the charactor encoding
  objStream.Position = 0
  objStream.Charset = "UTF-8"
 
  'Write to the steam
  objStream.WriteText pstrData
 
  'Save the stream to a file
  objStream.SaveToFile pstrFile
 
End Sub
0
 
bendeckoCommented:
This is for anyone who finds this on the 'net and needs a solution.

briankejser  answer worked for me.  I tried all the previous suggestions.

Thanks!

Bendecko

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now