Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the byte length of a string

Is there an easy way to get the byte length of a string in AS3? String.length works in many cases, but breaks if it encounters mulibyte unicode characters.

(in this particular case, I need to know this so I can preface messages sent across a TCP socket with the message length. This is in standard netstring format e.g. "length:message,").

like image 352
Ender Avatar asked Sep 04 '10 21:09

Ender


People also ask

How do you find the byte length of a string?

In Java, String. length() is to return the number of characters in the string, while String. getBytes(). length is to return the number of bytes to represent the string with the specified encoding.

How do you find the length of a string in a byte Python?

If you want the size of the string in bytes, you can use the getsizeof() method from the sys module.

How do you calculate byte size?

If the pixel dimensions are given, multiply them by each other and the bit depth to determine the number of bits in an image file. For instance, if a 24-bit image is captured with a digital camera with pixel dimensions of 2,048 x 3,072, then the file size equals (2048 x 3072 x 24)/8, or 18,874,368 bytes.

How long is a 16 byte string?

A 16 byte field can hold up to 16 ASCII characters, or perhaps 8 CJK glyphs that might encode a short kanji or hanzi password. "A 16 byte field can hold up to 16 ASCII characters, or perhaps 8 CJK glyphs that might encode a short kanji or hanzi password."


1 Answers

Use a ByteArray like so:

   var b:ByteArray = new ByteArray();
   b.writeUTFBytes("This is my test string");
   trace("Byte length: " + b.length);

Info on ByteArray here: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/ByteArray.html

like image 94
Paul Gregoire Avatar answered Oct 03 '22 20:10

Paul Gregoire