Open theory opened 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('');
};
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).
Using this function to convert a binary string to hex, I wrote this code to compare the output of
toHexString()
,toString()
, andtoBinaryString()
:The output was:
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 astoString()
does.