This is an odd one, but stick with me here. Given a hex value (from 00-FF), I need to convert it into binary, then from there convert it into an object with the key being the 'place' of each bit, and the value being a boolean.
That was an awkward explanation. Here's an example:
Given 'FF', I need to translate that into binary (parseInt(<hex value>, 16).toString(2)
works for that job).
Now that FF has bee 1111 1111
, I need to convert that into an object that looks like so:
{
"1": true,
"2": true,
"4": true,
"8": true,
"16":true,
"32":true,
"64": true,
"128": true
}
As another example, given the hex value 'A6', that bees 1010 0110
which would bee:
{
"1": false,
"2": true,
"4": true,
"8": false,
"16":false,
"32":true,
"64": false,
"128": true
}
The only thing I'm looking to do is that final conversion from binary -> object.
I know I can do it with a simple loop, but I was wondering if there's any cool p-sci way to acplish this.
Thanks!
This is an odd one, but stick with me here. Given a hex value (from 00-FF), I need to convert it into binary, then from there convert it into an object with the key being the 'place' of each bit, and the value being a boolean.
That was an awkward explanation. Here's an example:
Given 'FF', I need to translate that into binary (parseInt(<hex value>, 16).toString(2)
works for that job).
Now that FF has bee 1111 1111
, I need to convert that into an object that looks like so:
{
"1": true,
"2": true,
"4": true,
"8": true,
"16":true,
"32":true,
"64": true,
"128": true
}
As another example, given the hex value 'A6', that bees 1010 0110
which would bee:
{
"1": false,
"2": true,
"4": true,
"8": false,
"16":false,
"32":true,
"64": false,
"128": true
}
The only thing I'm looking to do is that final conversion from binary -> object.
I know I can do it with a simple loop, but I was wondering if there's any cool p-sci way to acplish this.
Thanks!
Share Improve this question asked May 31, 2016 at 22:11 Kieran EKieran E 3,6763 gold badges21 silver badges43 bronze badges 3- 1 I can think of many cool more plicated ways to do it, but a loop seems in order in this case. Just don't calculate the numeric keys every time, do it once and store them. – Eran Goldin Commented May 31, 2016 at 22:16
-
2
Note that you don't need to convert the value into a string:
x = parseInt('A6', 16) // 255; for (n = 1; x > 0; x = x >> 1, n = n * 2) { console.log(n, x % 2 == 1) }
– Hamms Commented May 31, 2016 at 22:23 - @Hamms You should really post that (with a little extra) as an answer. It's trivial to go from that to the object OP wants to generate. – Mike Cluck Commented May 31, 2016 at 22:33
4 Answers
Reset to default 5Note that, as I mentioned in a ment above, you don't need to convert the value to a string; if you keep it as a decimal integer, you can use the bitshift and remainder operators to get your binary values.
var x = parseInt('A6', 16); // 166
var result = {};
for (var n = 1; x > 0; x = x >> 1, n = n * 2) {
result[n] = x % 2 == 1;
}
console.log(JSON.stringify(result, undefined, 2));
You can do it without going to an intermediate binary string, but if you're just stuck on that final step you can do something like the following:
var bin = parseInt('0xa6', 16).toString(2);
Array.from(bin).reduce(function(byte, bit, index) {
byte[1 << bin.length - index - 1] = bit === '1';
return byte;
}, {});
JavaScript demo
Here's one long liner
const x = parseInt('A6', 16).toString(2).split('').reduceRight((last, bit, i, arr) => Object.assign(last, {[`${1 << (arr.length - i - 1)}`]: bit == 1}), {})
console.log(x)
It assumes that you're in an environment that supports puted keys (which is available in ES2015)
Here it is.
var x = {}, s = 1;
(0x10100110).toString(16).split('').reverse().map(a => {
x[s] = !!~~a;
s *= 2;
});