javascript - NodeJS Buffer - ASCII Binary Representation - Stack Overflow

number has num.toString(2)I'm wondering if there's a way to take a string such as 'Hello

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!

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!

Share Improve this question edited Jan 9, 2019 at 23:29 Patrick Roberts 52.1k10 gold badges117 silver badges163 bronze badges asked Jan 9, 2019 at 23:19 dardodardo 4,9705 gold badges38 silver badges52 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

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"

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745594757a4635041.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信