Text To Binary 05-17-2013, 12:18 AM
#1
Here's a quick string to binary converter I wrote using JS:
![[Image: 3vmHN0q.png]](http://i.imgur.com/3vmHN0q.png)
Webpage Code:
*I put the function in directly just because I didn't want to bother making everything on the webpage fancy and organized. If someone wants it better, they can improve it if they want.
Could be handy for someone to make a collection of these converters and save it to disk for a browser bookmark; convenience purposes.
Even hashing perhaps, not just conversions. We could come up with a collective bunch of things to put on a single webpage to be used as a tool.
The way this works is we split the string up into a character array, before passing it to map() which takes the augmented function for performing an action on every subsequent element of the provided array, before joining it all back together. If you want spaces in between each 8 bit binary string, just use join(' ') at the end instead.
For the function that is performed on each individual char in the string, we add the value of Math.pow(10,8) to the value of the floored value of the char code of the character in question cast to a base 2 value from the toString(2) function, and then cast this numeric value back to a string and call substring(1) on the result to get rid of the additional front '1' that we don't want. This '1' is essential when casting back to a string because if the number started with a 0, those would all get removed in it's numeric format.
for instance:
Would return '1', not '0001'. The value of Math.pow(10,8) is 100000000, meaning it won't affect any of the other values when added to the binary result of the char code because the remaining significant digits are all 0's.
edit: I guess instead of Math.pow() you could just use the value itself.. Doesn't really matter though much.
enjoy
moke:
![[Image: 3vmHN0q.png]](http://i.imgur.com/3vmHN0q.png)
Webpage Code:
Code:
<body>
<input id="strInput" type=inputfield onclick="" value="">
<input type=button onclick="javascript:document.getElementById('binOutput').value = document.getElementById('strInput').value.split('').map(function(n){return(Math.pow(10,8)+~~n.charCodeAt(0).toString(2)).toString().substring(1);}).join('')" value="Convert To Binary">
<textarea id="binOutput" style="width: 800px; padding: 5px; height: 300px; display: block; font-family:Sans-serif; font-size:9pt;"></textarea>
</body>*I put the function in directly just because I didn't want to bother making everything on the webpage fancy and organized. If someone wants it better, they can improve it if they want.
Could be handy for someone to make a collection of these converters and save it to disk for a browser bookmark; convenience purposes.
Even hashing perhaps, not just conversions. We could come up with a collective bunch of things to put on a single webpage to be used as a tool.The way this works is we split the string up into a character array, before passing it to map() which takes the augmented function for performing an action on every subsequent element of the provided array, before joining it all back together. If you want spaces in between each 8 bit binary string, just use join(' ') at the end instead.
For the function that is performed on each individual char in the string, we add the value of Math.pow(10,8) to the value of the floored value of the char code of the character in question cast to a base 2 value from the toString(2) function, and then cast this numeric value back to a string and call substring(1) on the result to get rid of the additional front '1' that we don't want. This '1' is essential when casting back to a string because if the number started with a 0, those would all get removed in it's numeric format.
for instance:
Code:
alert(0001.toString())Would return '1', not '0001'. The value of Math.pow(10,8) is 100000000, meaning it won't affect any of the other values when added to the binary result of the char code because the remaining significant digits are all 0's.
edit: I guess instead of Math.pow() you could just use the value itself.. Doesn't really matter though much.
enjoy
moke:
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)