Results 1 to 2 of 2

Thread: Help with String.fromCharCode()?

  1. #1
    Join Date
    Apr 2011

    Question Help with String.fromCharCode()?

    The following is a program where I have to convert from String to ASCII Code and then from ASCII Code back to String. This is what I have so far. The part where I convert to ASCII Code works perfectly. I don't have any problem. But when it comes to convert back to String is where I'm having a hard time. You can try the program by typing apple in the text area but it simply doesn't work. Can you please help me detect the problem? I would greatly appreciate it.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <script type="text/javascript">
    function thisFunction() {
            var firstMessage = document.thisForm.clearText0.value;
    	var someString = firstMessage;
    	var j;
    	for(j = 0; j < someString.length; j++) {
                    var thisArray=new Array();
    	        document.thisForm.clearText.value += thisArray[j] = someString.charCodeAt(j) + " ";
    //This is the section that is not working as it should...
    function thisFunction2() {
            var secondMessage = document.thisForm.clearText.value;
            var thisArray2 = new Array(secondMessage);
            for(var i = 0; i < thisArray2.length; i++) {
                var thisVariable = thisArray2[i].split(' ').join(',');
                document.thisForm.clearText2.value += String.fromCharCode(thisVariable);
    <form name="thisForm">
    Message to be encrypted...
    <textarea rows="6" cols="30" name="clearText0"></textarea>
    <input type="button" value="Convert to ASCII Code" onclick="thisFunction()"/>
    <textarea rows="6" cols="30" name="clearText"></textarea>
    <input type="button" value="Convert to String" onclick="thisFunction2()"/>
    <textarea rows="6" cols="30" name="clearText2"></textarea>

  2. #2
    Join Date
    Dec 2010
    Quote Originally Posted by ernesto1989 View Post
            var thisArray2 = new Array(secondMessage);
    That creates a single-element array. Just split the string directly, after losing any leading or trailing spaces:
    function thisFunction2() 
     var thisArray2 = document.thisForm.clearText.value.replace(/^\s*|\s*$/g, "").split(' ');
     for(var i = 0; i < thisArray2.length; i++)
      document.thisForm.clearText2.value += String.fromCharCode( thisArray2[ i ] );

    Q: I found this code...
    A: Then find its author.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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