Link to home
Start Free TrialLog in
Avatar of rascal
rascalFlag for United States of America

asked on

How to convert a UNICODE string to ansi in ASP?

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!
ASKER CERTIFIED SOLUTION
Avatar of prachwal
prachwal
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Anthony Perkins
Avatar of rascal

ASKER

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)