简体   繁体   English

ActionScript 3:ByteArray转换为二进制字符串

[英]ActionScript 3: ByteArray to binary String

I've been asked to implement and MD5 hasher ActionScript-3 and as I was in the middle of debugging how I formatted my input I came across a problem. 我被要求实施MD5哈希器ActionScript-3,并且在调试调试输入格式的过程中遇到了一个问题。 When I try and output the ByteArray as a binary string using .toString(2), the toString(2) method will perform some short cuts that alter how the binary should look. 当我尝试使用.toString(2)将ByteArray输出为二进制字符串时,toString(2)方法将执行一些捷径,以改变二进制文件的外观。 For Example 例如

var bytes:ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.writeUTFBytes("a");
bytes.writeByte(0x0);
var t1:String = bytes[0].toString(2); // is 1100001 when it should be 01100001
var t2:String = bytes[1].toString(2); // is 0 when it should be 00000000

so I guess my question is, might there a way to output a binary String from a ByteArray that will always shows each byte as a 8 bit block? 所以我想我的问题是,是否有办法从ByteArray输出二进制字符串,该字符串始终将每个字节显示为8位块?

All you need is to pad the output of toString(2) with zeros on the left to make its length equal to 8. Use this function for padding 您所需要做的就是在toString(2)的输出左侧填充零以使其长度等于8。使用此函数进行填充

function padString(str:String, len:int, char:String, padLeft:Boolean = true):String{
    var padLength:int = len - str.length;
    var str_padding:String = "";
    if(padLength > 0 && char.length == 1)
        for(var i:int = 0; i < padLength; i++)
            str_padding += char;

    return (padLeft ? str_padding : "") + str + (!padLeft ? str_padding: "");
}

With this function the code looks like this and gives the correct output 使用此功能,代码看起来像这样,并给出正确的输出

var bytes:ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.writeUTFBytes("a");
bytes.writeByte(0x0);
var t1:String = padString(bytes[0].toString(2), 8, "0"); // is now 01100001
var t2:String = padString(bytes[1].toString(2), 8, "0"); // is now 00000000

Update 更新资料
If you want to get a string representation of complete byteArray you can use a function which iterates on the byteArray. 如果要获取完整的byteArray的字符串表示形式,可以使用在byteArray上迭代的函数。 I have wrote the following function and it seems to work correctly. 我已经编写了以下函数,它似乎可以正常工作。 Give it a try 试试看

// String Padding function
function padString(str:String, len:int, char:String, padLeft:Boolean = true):String{
    // get no of padding characters needed
    var padLength:int = len - str.length;

    // padding string
    var str_padding:String = "";

    // loop from 0 to no of padding characters needed
    // Note: this loop will not run if padLength is less than 1 
    // as i < padLength will be false from begining
    for(var i:int = 0; i < padLength; i++)
        str_padding += char;

    // return string with padding attached either to left or right depending on the padLeft Boolean
    return (padLeft ? str_padding : "") + str + (!padLeft ? str_padding: "");
}

// Return a Binary String Representation of a byte Array
function byteArrayToBinaryString(bArray:ByteArray):String{
    // binary string to return
    var str:String = "";

    // store length so that it is not recomputed on every loop
    var aLen = bArray.length;

    // loop over all available bytes and concatenate the padded string to return string
    for(var i:int = 0; i < aLen; i++)
        str += padString(bArray[i].toString(2), 8, "0");

    // return binary string
    return str;
}

Now you can simply use the byteArrayToBinaryString() function like this: 现在,您可以像这样简单地使用byteArrayToBinaryString()函数:

// init byte array and set Endianness
var bytes:ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;

// write some data to byte array
bytes.writeUTFBytes("a");
bytes.writeByte(0x0);

// convert to binaryString
var byteStr:String = byteArrayToBinaryString(bytes); // returns 0110000100000000

Here is a function extended on the Hurlant library to handle hashing byteArray. Hurlant库上扩展的功能,用于处理散列byteArray。
This class has a learning curve but once you get it you will love it. 这门课有一个学习曲线,但是一旦获得,就会爱上它。

As far as your ByteArray issue with toString. 至于您的ByteArray与toString有关。 I know the toString method is not accurate For this very reason. 因为这个原因,我知道toString方法不准确。
You might want to look into byteArray.readMultiByte that will give you the 01 you are looking for. 您可能想要查看byteArray.readMultiByte,它将为您提供所需的01。 Although I can't seem top get it to work on my sample code either lol 尽管我似乎都无法将其用于我的示例代码,但大声笑
I just always get a and empty string. 我只是总是得到一个空字符串。

var bytes:ByteArray = new ByteArray();
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.writeUTFBytes("a");
bytes.writeByte(0x0);
bytes.position = 0
var t1:String = bytes.readMultiByte(1,'us-ascii'); // is 1100001 when it should be 01100001
trace(t1)
var t2:String = bytes.readMultiByte(1,'iso-8859-01'); // is 0 when it should be 00000000
trace(t2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM