bramstein / bit-array

JavaScript implementation of bit arrays.
78 stars 18 forks source link

Binary and Hex Values Disagree #10

Open theory opened 10 years ago

theory commented 10 years ago

Using this function to convert a binary string to hex, I wrote this code to compare the output of toHexString(), toString(), and toBinaryString():

var bits = new BitArray(256);
[
    4, 6, 32, 53, 67, 89, 104, 126, 157, 179, 190, 201, 212, 213, 227, 238, 242
].forEach( function (i) {
    bits.set(i, true);
});
console.log('Bits:    ' + bits.toString());
console.log('Binary:  ' + bits.toBinaryString());
console.log('Hex:     ' + bits.toHexString());
console.log('BitsHex: ' + binaryToHex(bits.toString()));
console.log('BinHex:  ' + binaryToHex(bits.toBinaryString()));

The output was:

Bits:    0000101000000000000000000000000010000000000000000000010000000000000100000000000000000000010000000000000010000000000000000000001000000000000000000000000000000100000000000000000000010000000000100000000001000000000011000000000000010000000000100010000000000000
Binary:  0000000000000100010000000000100000000000001100000000001000000000010000000000100000000000000000000010000000000000000000000000000001000000000000000000000100000000000000100000000000000000000010000000000000100000000000000000000100000000000000000000000001010000
Hex:     0000005000200001020000084000010020000000400800000030020000044008
BitsHex: 0A000000800004001000004000800002000000040000100200400C0010022000
BinHex:  0004400800300200400800002000000040000100020000080020000100000050

The curious thing is that the Hex output matches neither the BitHex nor the BinHex output. I plugged the Bits and Binary strings into a quick Perl script to see what hex would be generated, and they agreed with the output of BitsHex and BinHex. So I don't think that toHexString() is outputting bytes in the correct order. I suspect it should output them in the same order as toString() does.

theory commented 10 years ago

Actually, looking at those outputs again, the bytes output by toHexString() appear to be in reverse order to those output by toBinaryString(). You can get them to be the same with this change:

diff --git a/lib/bit-array.js b/lib/bit-array.js
index fa88936..9f8a461 100644
--- a/lib/bit-array.js
+++ b/lib/bit-array.js
@@ -137,7 +137,7 @@ BitArray.prototype.toHexString = function () {
   var result = [];

   for (var i = 0; i < this.values.length; i += 1) {
-    result.push(('00000000' + (this.values[i] >>> 0).toString(16)).slice(-8));
+    result.unshift(('00000000' + (this.values[i] >>> 0).toString(16)).slice(-8));
   }
   return result.join('');
 };
diff --git a/lib/bit-typed-array.js b/lib/bit-typed-array.js
index 5537857..2e58cbc 100644
--- a/lib/bit-typed-array.js
+++ b/lib/bit-typed-array.js
@@ -148,7 +148,7 @@ BitArray.prototype.toHexString = function () {

     for (var i = 0; i < this.wordArray.length; i += 1) {
         //result.push(this.wordArray[i].toString(16));
-        result.push(('00000000' + (this.wordArray[i] >>> 0).toString(16)).slice(-8));
+        result.unshift(('00000000' + (this.wordArray[i] >>> 0).toString(16)).slice(-8));
     }
     return result.join('');
 };
theory commented 10 years ago

Would also like to see a method that returns hex in the same order as the bytes returned by toString(), though. I think that’s more what is generally expected (it matches what I get from bit strings in other languages).