-
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
-
writeMultiByte as you've got it there should work according to the docs. But you might also try "unicode" instead of "utf-16".
-
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.save( utf16, fileName );
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
-
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)
-
Senior Member
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|