Read UTF8 Bytes


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // Handy ActionScript function that reports the number of bytes in a UTF-8 encoded string (the default encoding for ActionScript )
  2.  
  3. public function getNumBytesUTF8 (s:String):Number {
  4. var byteArray:ByteArray = new ByteArray();
  5. byteArray.writeUTFBytes(s);
  6. return byteArray.length;
  7. }
  8. // Usage:
  9. trace(getNumBytesUTF8("automobile")); // 10
  10. trace(getNumBytesUTF8("車")); // 3
  11.  
  12. // Note: "number of bytes" is not the same thing as "number of characters". For "number of characters," use the String class's length variable:
  13.  
  14. trace("車".length); // 1

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.