We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

lfatr on 06/12/08


Tagged

utf8


Versions (?)


encode_utf8


Published in: JavaScript 


from the german selfhtml website

  1. function encode_utf8(rohtext) {
  2. // dient der Normalisierung des Zeilenumbruchs
  3. rohtext = rohtext.replace(/
  4. /g,"\n");
  5. var utftext = "";
  6. for(var n=0; n<rohtext.length; n++)
  7. {
  8. // ermitteln des Unicodes des aktuellen Zeichens
  9. var c=rohtext.charCodeAt(n);
  10. // alle Zeichen von 0-127 => 1byte
  11. if (c<128)
  12. utftext += String.fromCharCode(c);
  13. // alle Zeichen von 127 bis 2047 => 2byte
  14. else if((c>127) && (c<2048)) {
  15. utftext += String.fromCharCode((c>>6)|192);
  16. utftext += String.fromCharCode((c&63)|128);}
  17. // alle Zeichen von 2048 bis 66536 => 3byte
  18. else {
  19. utftext += String.fromCharCode((c>>12)|224);
  20. utftext += String.fromCharCode(((c>>6)&63)|128);
  21. utftext += String.fromCharCode((c&63)|128);}
  22. }
  23. return utftext;
  24. }

Report this snippet 

You need to login to post a comment.