Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

readUTFBytes() not returning the correct characters

New Here ,
Apr 28, 2014 Apr 28, 2014

I am reading in a text file using FileReference() in a small AIR app i'm buliding.  This text file contains translations for the site I'm working on, in a bunch of different languages.  I correctly receive the byteArray in the data parameter of the FileReference object I created.  I am using readUTFBytes() to convert the entire data block to a String so I can parse through it and create an XML for all the different phrases and such.  I am finding that most characters are read correctly into the String except for example Ž (utf-8 code 142).  I trace the index value in the byteArray and correctly get the binary value 10001110 (142).  If I try and readUTFBytes() on this byte individually I get nothing in the character result.

var ba:ByteArray = new ByteArray();

ba.writeBytes( _loadFile.data, 126, 1 );  //where 126 is the index of the character...yes I quintuple checked it's the correct index

ba.position = 0;  //not necessary but for safe measure

var char:String = ba.readUTFBytes(ba.bytesAvailable);

trace( char );

char traces blank.  And the local variable value in Flashdevelop and Flash Builder is "".

I've tried readMultiByte() with a bunch of different charSet types as well, but nothing has yielded the correct character for this value (and many others).

Has anybody ever worked with loaded text files with odd characters and run into (and hopefully solved) this issue?

Thanks

TOPICS
ActionScript
742
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2014 Apr 28, 2014

Since you mentioned the .data property I assume you're using URLLoader? Have you tried FileStream?

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html

You may have better luck with the multibyte support it has. Be sure to play around with the character set you specify to read multibyte. Even though you may think something is UTF-8 it could very well be a Windows native encoding.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 28, 2014 Apr 28, 2014

Hey sinious,

No I am not using URLLoader().  I am using FileReference().

public function startLoadingFile( mEvt:MouseEvent ):void

        {

            _loadFile = new FileReference();

            _loadFile.addEventListener(Event.SELECT, onFileSelected);

            var fileFilter:FileFilter = new FileFilter("Plain Text (*.txt)", ";*.txt");

            _loadFile.browse([fileFilter]);

        }

private function onFileSelected( evt:Event ):void

        {

            _loadFile.removeEventListener(Event.SELECT, onFileSelected);

            _loadFile.addEventListener(Event.COMPLETE, onFileLoaded );

            _loadFile.load();

        }

private function onFileLoaded( evt:Event ):void

        {

            _loadFile.removeEventListener(Event.COMPLETE, onFileLoaded );       

            var langText:String = _loadFile.data.readUTFBytes( _loadFile.data.bytesAvailable );   

     }

I'm doing this so the desktop application I'm making (using AIR) can be used to load any local translation text file and parse it's contents.

The readMultiByte functionality in FileStream is no different than the multibyte support for a standard byteArray.  You choose your length and then declare your character set.

I've tried the majority of relevant characrter sets as outlined on the Adobe page http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/charset-codes.html including "Windows-1252".  Th eother "Windowa-xxxx" are for Turkish and Thai and such.  These characters are all visible in the text file I'm reading from. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2014 Apr 28, 2014
LATEST

The readMultiByte functionality in FileStream is no different

I would hope it's centralized code myself but more often than not I've found differences where there shouldn't be. It's a very simple class, might be worth a try.

I really find it odd but I lose a lot of multibyte characters when I use UTF-8 in JSON strings I receive, and setting the encoding to Windows-1252 with the server on iso-8859-1 (when it reported UTF-8). I end up just changing sets around on either side until it just worked.

If you're not using URLLoader, you might as well try that in binary mode as well so flash doesn't attempt to convert the data at all.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines