I want JavaScript to translate text in a textarea into binary code.
For example, if a user types in "TEST" into the textarea, the value "01010100 01000101 01010011 01010100" should be returned.
I would like to avoid using a switch statement to assign each character a binary code value (e.g. case "T": return "01010100) or any other similar technique.
Here’s a JSFiddle to show what I mean. Is this possible in native JavaScript?
What you should do is convert every char using
charCodeAtfunction to get the Ascii Code in decimal. Then you can convert it to Binary value usingtoString(2):And here’s a fiddle: http://jsfiddle.net/fA24Y/1/