Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
You can't use ascii characters directly in flash. Use String.fromCharCode(250) for ú
stream.writeUTFBytes("crep" + String.fromCharCode(250) + "scolo");
Copy link to clipboard
Copied
Nope, that wasn't it. Still outputs crepúscolo.
I believe the string is treated the same by Flash whether its set directly, or indirectly using String.fromCharCode.
For example:
trace("crepúscolo");
outputs:
crepúscolo
Other ideas, anyone?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I'm afraid System.useCodePage did nothing.
Interesting that it works for you though. I'm on a MacBook, perhaps there's something going on with that...
I have tried writeMultiByte with charset of macintosh. This happens to be the result of File.systemCharset as well. This did produce different results, however it still wasn't right:
code:
stream.writeMultiByte("crepúscolo",File.systemCharset);
output in file:
crepœscolo
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
Wow you're really putting some effort into helping me out with this, thanks for that Harry. Was a good idea too. Unfortunately it produced the same results - just the first character output...
Copy link to clipboard
Copied
The plot thickens... Up until now, I've been using Flash on the Mac and FlashDevelop on the PC(on my Mac) to write the code, and then check the output file. But I'm actually seeing different results when I check the output file, depending on where i check it! Craziness... Decided a more systematic approach was required in analysing all of these results:
RESULTS
stream.writeMultiByte("crepúscolo","utf-16");
PC FlashDevelop c
PC Notepad crepuscolo
Mac TextEdit c
Flash readUTFBytes c
stream.writeMultiByte("crepúscolo","macintosh");
PC FlashDevelop crepuscolo
PC Notepad crepuscolo
Mac TextEdit crepuscolo
Flash readUTFBytes crepuscolo
stream.writeBytes(getByteArray("crepúscolo")); //your suggestion harry
PC FlashDevelop c
PC Notepad crepuscolo
Mac TextEdit crep˙scolo
Flash readUTFBytes c
stream.writeUTFBytes("crepúscolo");
PC FlashDevelop crepúscolo
PC Notepad crepúscolo
Mac TextEdit crep√∫scolo
Flash readUTFBytes crepúscolo
as the most important thing is whether it appears correctly when reloaded into flash, it appears as though the simplest solution was the best - writeUTFBytes, readUTFBytes. I'm not sure why i didn't see it to begin with, I must have been distracted by a different problem and assumed that was the problem as it wasn't appearing right in FlashDevelop.
So thankyou to all who have helped. SOLVED!
Copy link to clipboard
Copied
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");
Copy link to clipboard
Copied
Afraid I'd tried that one, and weird thing happens - it only outputs one character:
c
I have made three different tests with utf-16:
the way you suggested:
stream.writeMultiByte("crepúscolo", "utf-16");
without the ú in case that was causing the problem:
stream.writeMultiByte("crepuscolo","utf-16");
outputting characters indiviudally:
stream.writeMultiByte("c", "utf-16");
stream.writeMultiByte("r", "utf-16");
Each time it just outputs the first character...
Copy link to clipboard
Copied
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.