Skip navigation
Currently Being Moderated

FileStream writing strange characters

Apr 12, 2010 4:36 PM

I'm trying to write out some Spanish text using FileStream(AIR), but special Spanish characters are writing out strange.

Here's a stripped down basic version of the problem, for you to try at home...

 

import flash.filesystem.*;
import flash.net.FileFilter;
import flash.events.Event;

var fileToSave:File = new File();
fileToSave.browseForSave("Save");
fileToSave.addEventListener(Event.SELECT, fileSaveSelected);

function fileSaveSelected(event:Event):void {
     var stream:FileStream = new FileStream();
     stream.open(event.target as File, FileMode.WRITE);
     stream.writeUTFBytes("crepúscolo");
     stream.close();
}

 

This should write out the word "crepúscolo", instead it writes out "crepúscolo"...

 

I've tried other combinations, for example i've tried stream.writeMultiByte with a variety of charsets, sometimes giving different results, but never the desired result...

 

Ideas anyone?

 
Replies
  • Currently Being Moderated
    Apr 13, 2010 7:31 AM   in reply to Craig Grummitt-CfccSz

    You can't use ascii characters directly in flash. Use String.fromCharCode(250) for ú

     

    stream.writeUTFBytes("crep" + String.fromCharCode(250) + "scolo");

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2010 10:17 AM   in reply to Craig Grummitt-CfccSz

    Does it change if use alter System.useCodePage?

     

    Funny, your char is getting split into 2 bytes. Check this out:

    var u:int = 250; // code of ú: fa
    var a:int = 195; // code of Ã: c3
    var d:int = 186; // code of º: ba
    trace(u.toString(16), a.toString(16), d.toString(16));
    trace(encodeURI("ú")); // %C3%BA

     

    FYI, your code works as expected on my system (WinXP, US English)...

     

    Message was edited by: tedalde2  Added works on my system.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2010 10:24 PM   in reply to Craig Grummitt-CfccSz

    Edited. You are right, it does get treated the same in flash.

     

    In windows it must have detected that there was a UTF-16 character (the "ú") in the string. Although writeUTFBytes is being used only for UTF-8, it must have forced the whole input as UTF-16 because of "ú" while on Mac it strictly used UTF-8 encoding. The method writeMultiByte with charset = "utf-16" should remedy this.

     

    stream.writeMultiByte("crepúscolo", "utf-16");

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 13, 2010 11:15 PM   in reply to Craig Grummitt-CfccSz

    Hmmm that's weird.. coz the input parameter asked is a string yet it only writes the first character... I wish i had a Mac to test this... i'll try to research some more.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 14, 2010 2:59 AM   in reply to Craig Grummitt-CfccSz

    Try this. I forced encode each character as 2 bytes in a ByteArray and then write that with writeBytes.

     

    import flash.filesystem.*;

    import flash.net.FileFilter;

    import flash.events.Event;

     

    var fileToSave:File = new File();

    fileToSave.browseForSave("Save");

    fileToSave.addEventListener(Event.SELECT, fileSaveSelected);

     

    function fileSaveSelected(event:Event):void {

       var stream:FileStream = new FileStream();

       stream.open(event.target as File, FileMode.WRITE);

       stream.writeBytes(getByteArray("crepúscoloÃ"));

       stream.close();

    }

     

    function getByteArray(sString:String):ByteArray

    {

       var aByteArray:ByteArray = new ByteArray(); //Big Endian

       var iStrLen:uint = sString.length;

     

       for(var iIndex:uint = 0; iIndex < iStrLen; iIndex++) {

          var iCode:uint = sString.charCodeAt(iIndex);

          aByteArray.writeByte(iCode);

          aByteArray.writeByte(0);

       }

       return aByteArray;

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points