获取字符串的字节长度 [英] Get the byte length of a string

查看:145
本文介绍了获取字符串的字节长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来获取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

这篇关于获取字符串的字节长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆