使用场景

一个UCS2解码的函数,直接调用即可


  1. /*
  2. * @Author: Park
  3. * @Date: 2021-08-18 23:34:37
  4. * @contact: QQ:7323650
  5. */
  6. importClass(java.lang.StringBuffer) //需提前导入类
  7. function UCS2解码(str) {
  8. var length = str.length;
  9. var stringBuffer = new StringBuffer(length);
  10. var i = 0;
  11. while (i < length) {
  12. var i2 = i + 1;
  13. var charAt = str.charAt(i);
  14. if (charAt == '\\') {
  15. i = i2 + 1;
  16. var charAt2 = str.charAt(i2);
  17. if (charAt2 == 'u') {
  18. i2 = 0;
  19. var i3 = 0;
  20. while (i2 < 4) {
  21. var i4 = i + 1;
  22. charAt = str.charAt(i);
  23. switch (charAt) {
  24. case 48 /*48*/:
  25. case 49 /*49*/:
  26. case 50 /*50*/:
  27. case 51 /*51*/:
  28. case 52 /*52*/:
  29. case 53 /*53*/:
  30. case 54 /*54*/:
  31. case 55 /*55*/:
  32. case 56 /*56*/:
  33. case 57 /*57*/:
  34. i3 = ((i3 << 4) + charAt) - 48;
  35. break;
  36. default:
  37. switch (charAt) {
  38. case 65 /*65*/:
  39. case 66 /*66*/:
  40. case 67 /*67*/:
  41. case 68 /*68*/:
  42. case 69 /*69*/:
  43. case 70 /*70*/:
  44. i3 = (((i3 << 4) + 10) + charAt) - 65;
  45. break;
  46. default:
  47. switch (charAt) {
  48. case 'a':
  49. case 'b':
  50. case 'c':
  51. case 'd':
  52. case 'e':
  53. case 'f':
  54. i3 = (((i3 << 4) + 10) + charAt) - 97;
  55. break;
  56. default:
  57. throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
  58. }
  59. }
  60. }
  61. i2++;
  62. i = i4;
  63. }
  64. stringBuffer.append(i3);
  65. } else {
  66. if (charAt2 == 't') {
  67. charAt2 = '\t';
  68. } else if (charAt2 == 'r') {
  69. charAt2 = '\r';
  70. } else if (charAt2 == 'n') {
  71. charAt2 = '\n';
  72. } else if (charAt2 == 'f') {
  73. charAt2 = '\f';
  74. }
  75. stringBuffer.append(charAt2);
  76. }
  77. } else {
  78. stringBuffer.append(charAt);
  79. i = i2;
  80. }
  81. }
  82. return stringBuffer.toString();
  83. }