Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Buffer - ASCII Binary Representation

number has num.toString(2)

I'm wondering if there's a way to take a string such as 'Hello world' and convert it to its ASCII binary representation.

Thanks!

like image 287
dardo Avatar asked Dec 30 '25 01:12

dardo


1 Answers

You could utilize the charCodeAt() method.

First split the string, then map the characters to their respective character code using the charCodeAt method. From there, you can use .toString(2) to convert the integer to binary and the padStart() method to add leading zero padding.

'Hello world'.split('').map(c => c.charCodeAt().toString(2).padStart(8, '0')).join(' ');

Result:

"01001000 01100101 01101100 01101100 01101111 00100000 01110111 01101111 01110010 01101100 01100100"
like image 155
Josh Crozier Avatar answered Jan 01 '26 18:01

Josh Crozier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!