Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    lsd
    lsd is offline
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help turning string of numbers into letters!

    Hi, I'm very new to javascript, and I'm working on a simple script that will allow me to take a string of numbers, and convert it to a word. For example:

    I type 00010203 into a box, and it would return abcd.

    I've got the above to work, however..I'd like to add a couple special characters so that when the user types in a string that contains "32" it will return a ' ' (space). For example:

    000102033200010203 would return 'abcd abcd'

    Here's my code, as of now:

    Code:
    <script type="text/javascript">
    function show_prompt()
    {
    	var s = prompt("Please enter a string of numbers");
    	for (i = 0; i <=s.length; i = i+2)
    {
    var t = s.slice(i,i+2)
    var n = parseInt(t,10) + 97
    document.write (String.fromCharCode(n))
    }
    }
    
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Sounds a bit pointless but doubtless is a learning project.


    Code:
    <script type="text/javascript">
    function show_prompt() {
    var s = prompt("Please enter a string of numbers","");
    for (i = 0; i <=s.length; i = i+2) {
    var t = s.slice(i,i+2);
    if (t != 32) {
    var n = parseInt(t,10) + 97;
    }
    else {n=t}
    document.write (String.fromCharCode(n));
    }
    }
    
    show_prompt();
    </script>
    You need to check that the number of digits entered is even and within a valid range 00 to 25 (and 32)

    A historian and a psychologist are sitting outside at a nudist colony. Historian: "Have you read Marx?" Psychologist: "Yes, I think they're from the wicker chairs."
    Last edited by Philip M; 02-21-2012 at 06:45 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    lsd
    lsd is offline
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Sounds a bit pointless but doubtless is a learning project.


    Code:
    <script type="text/javascript">
    function show_prompt() {
    var s = prompt("Please enter a string of numbers","");
    for (i = 0; i <=s.length; i = i+2) {
    var t = s.slice(i,i+2);
    if (t != 32) {
    var n = parseInt(t,10) + 97;
    }
    else {n=t}
    document.write (String.fromCharCode(n));
    }
    }
    
    show_prompt();
    </script>
    You need to check that the number of digits entered is even and within a valid range 00 to 25 (and 32)

    A historian and a psychologist are sitting outside at a nudist colony. Historian: "Have you read Marx?" Psychologist: "Yes, I think they're from the wicker chairs."
    Thanks, but now..let's say I wanted to add another character..like a '?'. By looking at the code, I'm a little unsure of what it is that you've done exactly.

    Lets say 31 should print a '?' how would you do that with your above code?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by lsd View Post
    Thanks, but now..let's say I wanted to add another character..like a '?'. By looking at the code, I'm a little unsure of what it is that you've done exactly.

    Lets say 31 should print a '?' how would you do that with your above code?
    if (t != 32 && t !=31) {

    but the ASCII code for ? is 63, not 31. You could change 31 to 63 with code - but it becomes more pointless by the hour.

    If you are going to ask for more characters such as ! or $ - don't. It would need complete re-writing with a translate table.
    Last edited by Philip M; 02-21-2012 at 07:51 PM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 498 Times in 492 Posts

    Lightbulb Alternative code ...

    An alternative solution that uses '00' as a space,
    but can be easily changed to any other code.
    Code:
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    //               0         1         2         3         4         5         6         7
    //               012345678901234567890123456789012345678901234567890123456789012345678901234567
    var CharCodes = ' abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+=-';
    function showChars(info) {
      if ((info.length % 2) != 0) { alert('Invalid input pairs'); return; }
      var str = '';
      var p;
      for (var i=0; i<info.length; i=i+2) {
        tmp = info.charAt(i)+''+info.charAt(i+1);
        p = parseInt(tmp,10) % CharCodes.length;  // force to be within valid range
        str += CharCodes.charAt(p);
      }
      document.getElementById('outCodes').innerHTML = str;
    }
    window.onload = function() { document.getElementById('numStr').focus(); }
    </script>
    
    <style type="text/css">
    
    </style>
    </head>
    <body>
    Number input ('00', '01', '03', ... '77') <br>
    <input type="text" id="numStr" size="80"
     value="0102030400010203040063646566007071727374" onblur="showChars(this.value)">
    <div id="outCodes"></div>
    </body>
    </html>

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Posts
    452
    Thanks
    0
    Thanked 20 Times in 20 Posts
    I have the code for what you're attempting to do but currently I can't share it...it's part of an algorythm for a larger program I've been working on an off of for the last few years. [thought: maybe one day I'll get help to finish it and market it...hehehe]
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #7
    New Coder
    Join Date
    Oct 2011
    Location
    New york
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Lightbulb

    Hello...
    Google for Regex Cheatsheets and you can find more summaries.

    \S - Matches anything but white-space characters.
    \s - Matches any white-space character.
    \D - Matches anything except digits.
    \d - Matches any digit.
    \W - Matches any nonword character.
    \w - Matches any word character.

    myString.split("\\s+");
    this groups all whitespaces as a delimiter... so if i have the string "abcd[space][tab]abcd", this should produce the strings "abcd" and "abcd" and omit the empty space between the space and the tab.

    Regards,
    Jackie
    Meet me on My Facebook

    My program - Javascript Refresh Page


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •