A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Convert string to UTF16(LE)

  1. #1
    Allan Jardine
    Join Date
    Dec 2000
    Location
    Turtle hunting
    Posts
    192

    Convert string to UTF16(LE)

    Hello all,

    Is there a way to write a UTF-16 string into a ByteArray in Flash/AS3? Basically I have a string (var test:String="allan"; for example) and I would like to write that into a ByteArray with UTF-16(LE) encoding. In this case it would be "61 00 6C 00 6C 00 61 00 6E 00".

    I've tried using utf16le.writeMultiByte( clipText, "utf-16" ); but it just comes out with what appears to be UTF8 (or just straight ASCII given the test string).

    Many thanks,
    Allan

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    writeMultiByte as you've got it there should work according to the docs. But you might also try "unicode" instead of "utf-16".

  3. #3
    Allan Jardine
    Join Date
    Dec 2000
    Location
    Turtle hunting
    Posts
    192
    Hi 5TonsOfFlax,

    Thanks for the reply. I thought it should work based on the information in the documentation as well, but I can't seem to make it to the trick. Here is a little example that I just tried:

    PHP Code:
    var utf16:ByteArray = new ByteArray();
    utf16.writeMultiByte"allan""utf-16" );

    var 
    fileRef:FileReference = new FileReference();
    fileRef.saveutf16fileName ); 
    Which results in a file being saved with the following hex string:
    61 6C 6C 61 6E

    I've tried 'unicode' as well as you suggested (I noticed that the docs say utf-16 is just an alias for 'unicode') but with no avail.

    So I get the feeling that I'm "not quite getting something..." :-)

    Regards,
    Allan

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Try "unicode" in your second writeMultiByte param per http://livedocs.adobe.com/flash/9.0/...set-codes.html
    Please use [php] or [code] tags, and mark your threads resolved 8)

  5. #5
    Senior Member Genesis F5's Avatar
    Join Date
    Jan 2002
    Location
    Unallocated memory
    Posts
    1,845
    I've tried the "unicode" string, but it does the same thing. You can get an extra character out of this example:

    PHP Code:
    var ba:ByteArray = new ByteArray();
    ba.writeMultiByte(String.fromCharCode(255) + "test""utf-8");
    trace(ba.length); 
    Changing the endianess doesn't do anything either for UTF-16.

  6. #6
    Allan Jardine
    Join Date
    Dec 2000
    Location
    Turtle hunting
    Posts
    192
    Hi guys,

    @neznein9: I tried 'unicode' but hit exactly the same issue. The docs say that 'utf-16' is just an alias for 'unicode'

    @Genesis F5: Interesting example. I've been wondering if I might need to write a character encoding converter, which this proves is certainly possible, but it looks like the built in library functions should be able to do it for me (and probably a lot after in execution that I could do!).

    Thanks,
    Allain

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center