There is a binary array:
var BinArr:Array [ 1, 1, 1, 0, 1, 0, 0]; I need to create a 32-bit number or several numbers from this array if the array has more than 32 elements. The main problem is that Actionscript 2.0 does not support the uint type, and when I use the Number and the number is more than 31 bits, everything hangs and eventually it turns out to be -1, for example, this array of bits gives -1 after the hang:
var BinArr:Array [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ]; Here, by the way, is the function by which I convert the array into a number:
function binaryToDecimal(s:String):Number{ var n:Number = 0; for(var i:int=0;i<s.length;i++){ n+=Number(s.substr(i,1))<<(s.length-1-i) } return n; } I can not understand what the error is, because Number is Decimal , as far as I understood, so 32 bits should fit, but for some reason it does not fit, the maximum is that it works 31 bits.
UPDATE1: In general, I solved the problem, I do not hang up, I immediately get -1 as it should be according to the idea. Now the problem is in the inverse function, it normally converts the number back into a binary string, but does not want to convert a negative number, here is the function:
function decimalToBinary(num:Number):String{ var bin:String = ""; while (num) { bin = num % 2 + bin; num = Math.floor(num / 2); } return (bin) ? bin : String(0); }