最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - NodeJS Buffer - ASCII Binary Representation - Stack Overflow

matteradmin7PV0评论

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"
Post a comment

comment list (0)

  1. No comments so far