How to convert text to binary code in JavaScript?

后端 未结 13 1780
囚心锁ツ
囚心锁ツ 2020-11-28 05:00

Text to Binary Code

I want JavaScript to translate text in a textarea into binary code.

For example, if a user types in "TEST

13条回答
  •  迷失自我
    2020-11-28 05:26

    8-bit characters with leading 0

    'sometext'
            .split('')
            .map((char) => '00'.concat(char.charCodeAt(0).toString(2)).slice(-8))
            .join(' ');
    

    If you need 6 or 7 bit, just change .slice(-8)

提交回复
热议问题