Solved

Write File in UTF-8 Format

Posted on 2002-03-05
17
32,559 Views
Last Modified: 2011-08-18
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
Comment
Question by:anguslai
  • 6
  • 3
  • 2
  • +6
17 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 6842861
Have your tried StrConv(text1.text, vbUnicode)?
0
 
LVL 2

Author Comment

by:anguslai
ID: 6842862
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
 
LVL 2

Author Comment

by:anguslai
ID: 6842881
rspahitz, the problem is not String Convertion. Now the problem is File Format.
0
 
LVL 1

Expert Comment

by:lmanapure
ID: 6843454
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
 
LVL 2

Author Comment

by:anguslai
ID: 6844396
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
 
LVL 1

Expert Comment

by:lmanapure
ID: 6846277
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
 
LVL 2

Author Comment

by:anguslai
ID: 6848931
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
 
LVL 2

Author Comment

by:anguslai
ID: 6849080
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Accepted Solution

by:
rspahitz earned 100 total points
ID: 6850712
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
 
LVL 2

Author Comment

by:anguslai
ID: 6888465
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 6894343
So what did you get with 'StrConv(text1.text, vbUnicode)'?  UTF-somethingelse?
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7020415
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
 
LVL 27

Expert Comment

by:Asta Cu
ID: 7150031
Zero response, recommend that points go to rspahitz if by the time Moderators finalizes this, no added comment.
Thanks,
":0) Asta
0
 

Expert Comment

by:modulo
ID: 7624237
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0
 

Expert Comment

by:digitrio
ID: 7746848
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
 

Expert Comment

by:briankejser
ID: 8681538
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
 
LVL 1

Expert Comment

by:bendecko
ID: 14556529
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now