admin管理员组文章数量:1431943
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 badges1 Answer
Reset to default 10You 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"
本文标签: javascriptNodeJS BufferASCII Binary RepresentationStack Overflow
版权声明:本文标题:javascript - NodeJS Buffer - ASCII Binary Representation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745594756a2665407.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论