Convert a text into binary code in JavaScript
This post will discuss how to convert a text into binary code in JavaScript.
There are several methods to convert a text into binary code in JavaScript. Binary code is a way of representing data using only two symbols: 0 and 1. Each character in a text can be converted to a sequence of bits (binary digits) that correspond to its ASCII code, which is a numerical value that represents the character. Here are some of the methods that we can use:
1. Using charCodeAt() and toString() function
The charCodeAt() function returns the Unicode value of the character at a given index in the string, and the toString() function converts a number to a string with a specified base. We can use these functions to convert each character of the string to a binary string by calling charCodeAt() with the index of the character, and then calling toString() with 2 as the base argument. We can use a for loop to iterate over each character in the text and concatenate the binary strings together using the + operator with a space separator. For example, we can convert a text into binary code using charCodeAt() and toString() like this:
|
1 2 3 4 5 6 7 8 9 |
let text = "Hello"; let binary = ""; for (let i = 0; i < text.length; i++) { binary += text.charCodeAt(i).toString(2) + " "; } // binary is "1001000 1100101 1101100 1101100 1101111" console.log(binary); |
2. Using TextEncoder
Another way is to use the TextEncoder() constructor and the encode() function of the TextEncoder object. The TextEncoder() constructor creates a new TextEncoder object that can encode a string into a byte stream with UTF-8 encoding. The encode() function converts a string to a Uint8Array object, which represents an array of 8-bit unsigned integers. We can then convert each element of the Uint8Array object to a binary string using the toString() function with 2 as the base argument, and join them together using the + operator. For example, we can convert a text into binary code using TextEncoder() and encode() like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
let text = "Hello"; // bytes is Uint8Array(5) [ 72, 101, 108, 108, 111 ] let bytes = new TextEncoder().encode(text); let binary = ""; for (let i = 0; i < bytes.length; i++) { binary += bytes[i].toString(2) + " "; } // binary is "1001000 1100101 1101100 1101100 1101111" console.log(binary); |
3. Using replace() function
A third way is to use replace() function of the string object and a regular expression. The replace() function accepts a regular expression and a replacement string or function as arguments and gives us a new string that is the result of changing the matches of the regular expression with the replacement string or function. We can use a regular expression that matches any character in the string, such as /./g, and change it with its binary representation using a function that calls charCodeAt() and toString(). For instance, we can make a string binary using replace() like this:
|
1 2 3 4 5 6 7 8 |
let text = "Hello"; let binary = text.replace(/./g, function(char) { return char.charCodeAt(0).toString(2) + " "; }); // binary is "1001000 1100101 1101100 1101100 1101111" console.log(binary); |
That’s all about converting a text into binary code in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)