1
0

utf.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
  2. /* utf.js - UTF-8 <=> UTF-16 convertion
  3. *
  4. * Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
  5. * Version: 1.0
  6. * LastModified: Dec 25 1999
  7. * This library is free. You can redistribute it and/or modify it.
  8. */
  9. function Utf8ArrayToStr(array) {
  10. var out, i, len, c;
  11. var char2, char3;
  12. out = "";
  13. len = array.length;
  14. i = 0;
  15. while(i < len) {
  16. c = array[i++];
  17. switch(c >> 4)
  18. {
  19. case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  20. // 0xxxxxxx
  21. out += String.fromCharCode(c);
  22. break;
  23. case 12: case 13:
  24. // 110x xxxx 10xx xxxx
  25. char2 = array[i++];
  26. out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
  27. break;
  28. case 14:
  29. // 1110 xxxx 10xx xxxx 10xx xxxx
  30. char2 = array[i++];
  31. char3 = array[i++];
  32. out += String.fromCharCode(((c & 0x0F) << 12) |
  33. ((char2 & 0x3F) << 6) |
  34. ((char3 & 0x3F) << 0));
  35. break;
  36. }
  37. }
  38. return out;
  39. }