【问题标题】:ActionScript ByteArray manipulationActionScript ByteArray 操作
【发布时间】:2011-07-28 23:35:51
【问题描述】:

我有一些二进制数据,我无法将其存储在字符串中,因此我使用的是 ByteArray。 问题是我需要一些字符串附带的功能,具体来说我需要 charAt、substr、indexOf 和 substring 方法。

【问题讨论】:

  • 克里斯:你能添加一些关于你正在写入字节数组的内容的描述吗?我认为您需要知道字节数组的格式是什么,才能以您描述的方式对其进行搜索。例如:它是单个 AMF 对象吗?或者是几个字符串?还是一个对象,一个字符串和其他东西?
  • 字节数组包含php的serialize()函数的结果。序列化前的数据是这样的: Array(byte opcode, array(data, data, ...)) [注:嵌套数组的内容可以是任意8bit二进制数据,因此序列化输出也包含8bit任意二进制数据]

标签: apache-flex actionscript binary bytearray


【解决方案1】:

这些将很容易实现。如果需要,我可以发布代码。数据是字符串吗?还是需要将其视为任意二进制文件?无论哪种情况,字符的宽度是多少(例如 8 位、16 位)?

public static function charAt(bytes:ByteArray, index:int):String {
    if (bytes.length <= index) return null; 
    return String.fromCharCode(bytes[index]); 
} 

public static function substr(bytes:ByteArray, start:int, length:int=0):String { 
    var res:ByteArray = bytes.readBytes(bytes, start, length); 
    return res.toString(); 
} 

public static function substring(bytes:ByteArray, start:int, end:int=0):String { 
    return substr(bytes, start, end-start); 
}

public static function indexOf(bytes:ByteArray, str:String):int { 
    for (var i:int=0; i<bytes.length; i++) { 
        var strPos:int = 0; 
        while (String.fromCharCode(bytes[i+strPos]) == str.charAt(strPos)) { 
            strPos++; 
            if (strPos == str.length) return i; 
        } 
    } 
    return -1; 
}

【讨论】:

  • 数据包含任意二进制,所以我猜应该把整个东西当作任意数据,字符是8位宽。
  • 另一个问题——这些方法应该返回字符串还是新的字节数组?
  • public static function charAt(bytes:ByteArray, index:int):String { if (bytes.length &lt;= index) return null; return String.fromCharCode(bytes[index]); } public static function substr(bytes:ByteArray, start:int, length:int=0):String { var res:ByteArray = bytes.readBytes(bytes, start, length); return res.toString(); } public static function substring(bytes:ByteArray, start:int, end:int=0):String { return substr(bytes, start, end-start); }
  • public static function indexOf(bytes:ByteArray, str:String):int { for (var i:int=0; i&lt;bytes.length; i++) { var strPos:int = 0; while (String.fromCharCode(bytes[i+strPos]) == str.charAt(strPos)) { strPos++; if (strPos == str.length) return i; } } return -1; }
  • 感谢您的帮助,我们深表感谢。如果您需要任何 php 帮助,请联系我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2012-06-16
  • 2010-09-05
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多