Home > Default > How to save a UTF-8 encoded text file ?

How to save a UTF-8 encoded text file ?

October 11Hits:0
Advertisement
hi People
I have a little script which reads the source text from a layer and saves it to a .txt file. This is on a Mac and all was good until recently when I tried opening the .txt file on a PC in Notepad and found my ˚ degree symbols all whack.
Resaving the .txt file in TextEdit as Unicode (UTF-8) encoding solved the problem, now opens fine in Notepad.
But ideally I'd like the script to output the .txt as UTF-8 in the first place. It's currently Western (Mac OS Roman). I've tryed adding in myfile.encoding = "UTF8" but the resulting file is still Western (and the special charaters have wigged out again)
any help greatly appreciated../daniel
    var theComp = app.project.activeItem;
    var dataRO = theComp.layer("dataRO").sourceText;
    // prompt user to save file
    var theFile = new File ("~/Desktop/"+ theComp.name + "_output.txt");
    theFile = theFile.saveDlg("Save an ASCII export file.");
    if (theFile != null) {          // check user didn't cancel dialog
        theFile.lineFeed = "windows";
        //theFile.encoding = "UTF8";
        theFile.open("w","TEXT","????");
        theFile.writeln("move details:");
        theFile.writeln(dataRO.value.toString());
    theFile.close();

Answers

Hi,
Got it, it seems, the utf-8 standard use 2-bytes (and more) encoding on accents and special characters.
I found some info there with some code http://ivoronline.com/Coding/Theory/Tutorials/Encoding%20-%20Text%20-%20UTF%208.php
However there was some error so I fixed it. (However for 3 and 4 bytes characters i didnt test it. So maybe you'll have to change back the 0xbf to 0x3f or something else.)
So here is the code.
Header 1
function convertCharToUTF(character){
    var utfBytes = "";
    c = character.charCodeAt(0)
    if (c < 0x80) {
        utfBytes =  String.fromCharCode (c);
    else if (c < 0x800) {
        utfBytes =  String.fromCharCode (0xC0 | c>>6);
        utfBytes +=  String.fromCharCode (0x80 | c & 0xbF);
    else if (c < 0x10000) {
        utfBytes = String.fromCharCode (0xE0 | c>>12);
        utfBytes += String.fromCharCode (0x80 | c>>6 & 0xbF);
        utfBytes += String.fromCharCode (0x80 | c & 0xbF);
    else if (c < 0x200000) {
        utfBytes += String.fromCharCode (0xF0 | c>>18);
        utfBytes += String.fromCharCode (0x80 | c>>12 & 0xbF);
        utfBytes += String.fromCharCode (0x80 | c>>6 & 0xbF);
        utfBytes =+ String.fromCharCode (0x80 | c & 0xbF);
        return utfBytes
function convertStringToUTF(stringToConvert){
    var utfString = ""
    for (var i = 0 ; i < stringToConvert.length; i++){
        utfString = utfString + convertCharToUTF(stringToConvert.charAt (i))
    return utfString;
var theFile= new File("~/Desktop/_output.txt");
theFile.open("w", "TEXT");
theFile.encoding = "BINARY"
theFile.linefeed = "Unix"
theFile.write("");//or theFile.write(String.fromCharCode (0xEF) + String.fromCharCode (0xEB) + String.fromCharCode (0xBF)
theFile.write(convertStringToUTF("Your stuff éàçËôù"));
theFile.close();

Read other 8 answers

Tags:

Related Articles

  • How to save a UTF-8 encoded text file ?October 11

    hi People I have a little script which reads the source text from a layer and saves it to a .txt file. This is on a Mac and all was good until recently when I tried opening the .txt file on a PC in Notepad and found my ˚ degree symbols all whack. Res

  • How to read UTF-8 encoded text file randomly?October 11

    I am trying to read a text file which has been encoded in UTF-8. The problem is that I need to access the file randomly. The RandomAccessFile is a low-level class and there seems to be no-way to wrap it in InputStreamReader so that UTF-8 encoding can

  • How to save the RichTextBox Content in Text fileOctober 11

    hI , I NEED TO SAVE THE RichTextBox Content in text file ? If this IS a LabVIEW question, here's an example of how to load a rich text file (to help if you don't know how to get a rich text in LabVIEW). If you have a Rich Text Box, you can use the in

  • Not able to save report output in a text file and RTF fileOctober 11

    I am using Oracle Developer6.0 . I am facing problem with reports. I am invoking reports from form using run_product. 1. I could not able to save the report in text file . Whenever i try to save the report output in a text file.It gives dump and appl

  • How can I save an email as a text file before deleting from it my inbox?October 11

    Some emails I want to save to my computer so that i can view them without opening up my email. I used to be able to do this by saving to a documents folder as a text file. (Outlook express). I do not find this capablity in Thunderbird. if it is there

  • Save Digital Data to a Text FileOctober 11

    I have digital data collected from two ports simultaneously as N number of samples in waveform that I do calculations on to get a result. I would like to save the raw data in a text file so that it can be easily viewed in an external program, like Ex

  • Save listview item in a text file VS 2010 C++November 30

    is there anyway that I can save the item in the `Listview` into a text file?? For example..  col.1 | col.2 | col3. | col.4 | col.5 Data | Data | Data | Data | Data  Data | Data | Data | Data | DataNot out of the box but you could iterate through the

  • Is there an iPad app that will let me read a pdf be able to type notes that I can save as a separate plain text file?October 11

    In researching a subject, I would like to be able to read several pdfs in succession and keep adding to a text document where I keep notes. I have seen iPad apps that will let you mark up a pdf, but I would like to have one text file that I keep all

  • [SOLVED] Can't save UTF-8 encoded source file in IDEOctober 11

    I had a problem where I could not save a c++ source file from anjuta using the gtksourceview plugin, but could using the scintilla plugin.  I would get an error message with "invalid byte sequence in conversion input" in the text.  Eclipse CDT a

  • Reading UTF-8 Encoding xml file sqlserverOctober 11

    Hi , I am recieving a xml file from a third party vendor. it is encoded in UTF-8. while i am reading it i am getting the below error. Msg 9420, Level 16, State 1, Line 3 XML parsing: line 30117390, character 33, illegal xml character the characters c

Copyright (C) 2019 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 14 q. 0.622 s.