Is there an elegant way to convert some string to binary and get a result with a fixed length?
The built-in function is: parseInt(str, 10).toString(2), but it's cutting the length.
For example, if I want length = 8 bits, then myFunction("25") will return 00011001 instead of just 11001.
I know that I can append zeros to the beginning, but it doesn't seems like an elegant way to me.
In modern JavaScript:
const number = 25;
number.toString(2).padStart(8, '0'); // '00011001'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With