Skip to content

Instantly share code, notes, and snippets.

@codewithpom
Created November 19, 2021 09:22
Show Gist options
  • Save codewithpom/fefe531cdc79c2ace95a7d42f0b575c3 to your computer and use it in GitHub Desktop.
Save codewithpom/fefe531cdc79c2ace95a7d42f0b575c3 to your computer and use it in GitHub Desktop.
Converts a word or sentence to binary
function convertToBinary(text) {
var result = '';
for (var i = 0; i < text.length; i++) {
var char = text[i];
var code = char.charCodeAt(0);
result += code.toString(2) + ' ';
}
return result;
}
const string = "Hello My name is Padmashree"
const binary = convertToBinary(string)
console.log(binary)
/*
1001000 1100101 1101100 1101100 1101111 100000 1001101 1111001 100000 1101110 1100001 1101101 1100101 100000 1101001 1110011 100000 1010000 1100001 1100100 1101101 1100001 1110011 1101000 1110010 1100101 1100101
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment