Login Register






Text To Binary filter_list
Author
Message
Text To Binary #1
Here's a quick string to binary converter I wrote using JS:
[Image: 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. Wink 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 Confusedmoke:
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

Text To Binary #2
Here's a quick string to binary converter I wrote using JS:
[Image: 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. Wink 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 Confusedmoke:
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Text To Binary #3
It seems this thread is going unnoticed or being ignored too lol. The developers here are just too quiet... Yawn
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Text To Binary #4
It seems this thread is going unnoticed or being ignored too lol. The developers here are just too quiet... Yawn
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Text To Binary #5
(05-17-2013, 09:07 PM)ArkPhaze Wrote: It seems this thread is going unnoticed or being ignored too lol. The developers here are just too quiet... Yawn

Well I had seen this post of yours from my phone. But I didn't replied as I usually have problems replying from my cell phone (Bad network 2G data). But its a nice tutorial Sire.

I have a bit problem in understanding the following part do help me out :-

Quote: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.
[Image: OilyCostlyEwe.gif]

Reply

RE: Text To Binary #6
(05-17-2013, 09:07 PM)ArkPhaze Wrote: It seems this thread is going unnoticed or being ignored too lol. The developers here are just too quiet... Yawn

Well I had seen this post of yours from my phone. But I didn't replied as I usually have problems replying from my cell phone (Bad network 2G data). But its a nice tutorial Sire.

I have a bit problem in understanding the following part do help me out :-

Quote: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.
[Image: OilyCostlyEwe.gif]

Reply

RE: Text To Binary #7
(05-17-2013, 09:21 PM)Feurex Wrote:
(05-17-2013, 09:07 PM)ArkPhaze Wrote: It seems this thread is going unnoticed or being ignored too lol. The developers here are just too quiet... Yawn

Well I had seen this post of yours from my phone. But I didn't replied as I usually have problems replying from my cell phone (Bad network 2G data). But its a nice tutorial Sire.

I have a bit problem in understanding the following part do help me out :-

Quote: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.

Code:
function(n){return(Math.​pow(10,8)+~~n.charCodeAt(0).toString(2)).toString().substring(1);}

That's this part.

Math.pow(10,8) is added to the floored value of the char code, converted to base 2, from the base 10 value, then after the entire thing is cast to a string, we remove the starting '1' by using substring(1).

So for example, the addition is:
100000000 + 00101011 = 100101011
'100101011' is 9 characters, and we only want 8, for an 8 bit value, we use substring(1) to remove the one at the beginning.

'100101011' = '00101011'
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Text To Binary #8
(05-17-2013, 09:21 PM)Feurex Wrote:
(05-17-2013, 09:07 PM)ArkPhaze Wrote: It seems this thread is going unnoticed or being ignored too lol. The developers here are just too quiet... Yawn

Well I had seen this post of yours from my phone. But I didn't replied as I usually have problems replying from my cell phone (Bad network 2G data). But its a nice tutorial Sire.

I have a bit problem in understanding the following part do help me out :-

Quote: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.

Code:
function(n){return(Math.​pow(10,8)+~~n.charCodeAt(0).toString(2)).toString().substring(1);}

That's this part.

Math.pow(10,8) is added to the floored value of the char code, converted to base 2, from the base 10 value, then after the entire thing is cast to a string, we remove the starting '1' by using substring(1).

So for example, the addition is:
100000000 + 00101011 = 100101011
'100101011' is 9 characters, and we only want 8, for an 8 bit value, we use substring(1) to remove the one at the beginning.

'100101011' = '00101011'
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply