Skip to content

Binary and Hex Values Disagree #10

@theory

Description

@theory

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions