javascript - Letters to predefined numbers conversion in one window -


) have searched high , low, can´t find need. or i´m stupid right ;-)

i need page several input boxes can type text, , output area below each input, shows text converted predefined numbers.

example:

input: abcde fghi æøå (i need kinds of characters .,/: etc.)

output: 064 065 066 067 068 032

so needs convert this:

"a"="064 "

"b"="065 "

"space"="032 "

(and yes, each number in output needs separated, or space added after each number)

i have tried different cipher guides in both php , javascript, can´t work. did excel document of it, had limited amount of characters convert, started behaving weird. thought maybe php answer!

any appreciated /rasmus

in spirit of elclanrs deleted answer, , posterity:

<script>  // using standard loop function stringtocharcodes(s) {     var result = [];      function pad(n){ return (n<10? '00' : n<100? '0' : 0) + n;}      (var i=0, ilen=s.length; i<ilen; i++) {         result.push(pad(s.charcodeat(i)));     }     return result.join(' '); }  // using es5 foreach function stringtocharcodes2(s) {     var result = [];      function pad(n){ return (n<10? '00' : n<100? '0' : 0) + n;}      s.split('').foreach(function(a){result.push(pad(a.charcodeat(0)))});      return result.join(' '); } </script>  <input onkeyup="document.getelementbyid('s0').innerhtml = stringtocharcodes(this.value);"><br> <span id="s0"></span> 

edit

if want custom mapping, use object (i've included 2 characters, can add many want):

var mapchars = (function() {   var mapping = {'198':'019', '230':'018'};    return function (s) {     var c, result = [];      (var i=0, ilen=s.length; i<ilen; i++) {       c = s.charcodeat(i);       result.push(c in mapping? mapping[c] : c);     }      return result.join(' ');   } }());  alert(mapchars('Ææ')); //  

using character code mapping seems reasonable solution, using actual character may subject different page character encoding.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -