Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

char 2 bin

Status
Not open for further replies.

mpalmer12345

Programmer
Feb 16, 2004
59
US
After a bit of searching, I give up! How do I convert an ASCII value to its numeric equivalent (preferably binary) in Javascript? The result should cover all values from 0 to 255, not just A-Za-z.
 
Use the fromCharCode method of the String object.

To see this in action, paste the following into your address bar:

Code:
javascript:alert( String.fromCharCode(68, 97, 110) );

Hope this helps!
Dan
 
Hm, thanks, DAN, but I want to convert an Ascii letter into a binary #, not the other way around. Maybe there's a toCharCode out there?
 
Sure... here are some examples. As you can see, the first 2 examples give the same result.

Code:
var myString="Dan";
alert(myString.charCodeAt()); // returns 68
alert(myString.charCodeAt(0)); // returns 68
alert(myString.charCodeAt(1)); // returns 97
alert(myString.charCodeAt(2)); // returns 110

Jeff
 

Aaah - I misread your post, and got it the wrong way round... Use charCodeAt, as Jeff suggests, to do the job you're after.

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top