IT_Programming/JavaScript

문자열을 아스키 코드로 만들기

JJun ™ 2007. 9. 30. 11:17

<html>
<head>
 <script language="JavaScript">
 
  var str_in;
  var str_out = "";
  var num_in;
  var num_out = "";
  var e = "문자열을 입력하세요!";
  
  function str_to_num(form) {
   num_out = "";
   if(form.input.value == "") alert(e);
   else {
    str_in = escape(form.input.value);
    
    for(i = 0; i < str_in.length; i++) {
     num_out += str_in.charCodeAt(i) - 23;
    }

    form.output.value = num_out;
    form.input.value = "";
   }
  }

  function num_to_str(form) {
   str_out = "";
   if(form.output.value == "") alert(e)
   else {
    num_out = form.output.value; 
    
    for(i = 0; i < num_out.length; i += 2) {
     num_in = parseInt(num_out.substr(i,[2])) + 23;
     num_in = unescape('%' + num_in.toString(16));
     str_out += num_in;
    }
    
    form.input.value = unescape(str_out);
    form.output.value = "";
   }
  }
 
 </script>
</head>
<body>

 <form name=encryptform>
 <table>
   <tr>
    <td align=center>원본 문자열</td>
    <td> </td>
    <td align=center>암호화된 코드 </td>
   </tr>
   <tr>
    <td align=center><input name=input type=text size=40 value="암호화 된 코드"></td>
    <td align=center>
    <input type=button value="<--" onClick="javascript:num_to_str(this.form)"><br>
    <input type=button value="-->" onClick="javascript:str_to_num(this.form)">
    </td>
    <td align=center><input name=output type=text size=40></td>
   </tr>
  </table>
 </form>

</body>
</html>