问题描述
是否有一种简单的方法来获取AS3中字符串的字节长度? String.length在许多情况下都可以使用,但是遇到多字节unicode字符时会中断.
(在这种情况下,我需要知道这一点,以便我可以在TCP套接字上发送的消息的开头带有消息长度.这是标准的netstring格式,例如"length:message,".)
使用ByteArray像这样:
此处有关ByteArray的信息: http://www.adobe. com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/ByteArray.html
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,").
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
这篇关于获取字符串的字节长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!