How to convert a UNICODE string to ansi in ASP?

Posted on 2009-05-02
Last Modified: 2012-05-06
We are reading a SQL Server 2000 database and writing its contents out to a text file via our ASP web page.

The problem is that because the fields are defined as ntext, the resulting file is written out as unicode (we're just using the Scripting.FileSystemObject to create a text file and it appears to automatically be saving it as a unicode file).

This is a problem because the 3rd party system we send the text file off to cannot process the unicode data.

We tried casting the SQL field to text (from its original unicode ntext type) but the resulting string was still unicode.

QUESTION: Is there a way in ASP to convert a UNICODE string to ANSI prior to writing it out to the file?

Thanks experts!
Question by:rascal
    LVL 3

    Accepted Solution

    LVL 75

    Expert Comment

    by:Anthony Perkins
    Since this is a duplicate question for:
    Please request that it be deleted.
    LVL 1

    Author Comment

    Actually the above solution didn't actually fix the problem (which I hadn't fully realized)
    I had to explicitly open the text file in ANSI mode in order to get the resulting file to be ANSI. Just relying on the default open mode for the file still resulted in Unicode.
    Here is the code I used:

     dim f
     const ForReading = 1
     const ForWriting = 2
     const ForAppending = 8
        const TristateUseDefault = -2
     const TristateTrue = -1
     const TristateFalse = 0
        objFSO.CreateTextFile sOutputFilename, 2, true   ' 2 = for writing, true = create
     set f = objFSO.GetFile(sOutputFilename)
        set objFile = f.OpenAsTextStream(ForWriting, TristateFalse)

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    729 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

    17 Experts available now in Live!

    Get 1:1 Help Now