escape 和 unescape


原理:对除ASCII字母、数字、标点符号 @ * _ + - . / 以外的其他字符进行编码

escape(string)
escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

  1. escape("Kyle Thanas!测试")
  2. "Kyle%20Thanas%21%u6D4B%u8BD5"

unescape(string)
unescape() 函数可对通过 escape() 编码的字符串进行解码。

  1. unescape("Kyle%20Thanas%21%u6D4B%u8BD5")
  2. "Kyle Thanas!测试"

encodeURI 和 decodeURI


原理:返回编码为有效的统一资源标识符 (URI) 的字符串,不会被编码的字符:! @ # $ & * ( ) = : / ; ? + ‘

encodeURI(URIstring)
encodeURI() 函数可把字符串作为 URI 进行编码。

  1. encodeURI("https://www.baidu.com/s?wd=编码")
  2. "https://www.baidu.com/s?wd=%E7%BC%96%E7%A0%81"

decodeURI(URIstring)
decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码。

  1. decodeURI("https://www.baidu.com/s?wd=%E7%BC%96%E7%A0%81")
  2. "https://www.baidu.com/s?wd=编码"

encodeURIComponent 和 decodeURIComponent


原理:对URL的组成部分进行个别编码,而不用于对整个URL进行编码

encodeURIComponent(URIstring)
encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。

  1. encodeURIComponent("https://coding.net/u/KyleThanas")
  2. "https%3A%2F%2Fcoding.net%2Fu%2FKyleThanas"

decodeURIComponent(URIstring)
decodeURIComponent() 函数可对 encodeURIComponent() 函数编码的 URI 进行解码。

  1. decodeURIComponent("https%3A%2F%2Fcoding.net%2Fu%2FKyleThanas")
  2. "https://coding.net/u/KyleThanas"

js中的BASE64算法


  1. var Base64 = {
  2. table: [
  3. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  4. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  5. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  6. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  7. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  8. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  9. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  10. '4', '5', '6', '7', '8', '9', '+', '/'
  11. ],
  12. UTF16ToUTF8: function (str) {
  13. var res = [],
  14. len = str.length;
  15. for (var i = 0; i < len; i++) {
  16. var code = str.charCodeAt(i);
  17. if (code > 0x0000 && code <= 0x007F) {
  18. res.push(str.charAt(i));
  19. } else if (code >= 0x0080 && code <= 0x07FF) {
  20. var byte1 = 0xC0 | ((code >> 6) & 0x1F);
  21. var byte2 = 0x80 | (code & 0x3F);
  22. res.push(
  23. String.fromCharCode(byte1),
  24. String.fromCharCode(byte2)
  25. );
  26. } else if (code >= 0x0800 && code <= 0xFFFF) {
  27. var byte1 = 0xE0 | ((code >> 12) & 0x0F);
  28. var byte2 = 0x80 | ((code >> 6) & 0x3F);
  29. var byte3 = 0x80 | (code & 0x3F);
  30. res.push(
  31. String.fromCharCode(byte1),
  32. String.fromCharCode(byte2),
  33. String.fromCharCode(byte3)
  34. );
  35. } else {}
  36. }
  37. return res.join('');
  38. },
  39. UTF8ToUTF16: function (str) {
  40. var res = [],
  41. len = str.length;
  42. var i = 0;
  43. for (var i = 0; i < len; i++) {
  44. var code = str.charCodeAt(i);
  45. if (((code >> 7) & 0xFF) == 0x0) {
  46. res.push(str.charAt(i));
  47. } else if (((code >> 5) & 0xFF) == 0x6) {
  48. var code2 = str.charCodeAt(++i);
  49. var byte1 = (code & 0x1F) << 6;
  50. var byte2 = code2 & 0x3F;
  51. var utf16 = byte1 | byte2;
  52. res.push(Sting.fromCharCode(utf16));
  53. } else if (((code >> 4) & 0xFF) == 0xE) {
  54. var code2 = str.charCodeAt(++i);
  55. var code3 = str.charCodeAt(++i);
  56. var byte1 = (code << 4) | ((code2 >> 2) & 0x0F);
  57. var byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);
  58. utf16 = ((byte1 & 0x00FF) << 8) | byte2
  59. res.push(String.fromCharCode(utf16));
  60. } else {}
  61. }
  62. return res.join('');
  63. },
  64. encode: function (str) {
  65. !str ? '' : 1;
  66. var utf8 = this.UTF16ToUTF8(str),
  67. i = 0,
  68. res = [];
  69. var len = utf8.length;
  70. while (i < len) {
  71. var c1 = utf8.charCodeAt(i++) & 0xFF;
  72. res.push(this.table[c1 >> 2]);
  73. if (i == len) {
  74. res.push(this.table[(c1 & 0x3) << 4]);
  75. res.push('==');
  76. break;
  77. }
  78. var c2 = utf8.charCodeAt(i++);
  79. if (i == len) {
  80. res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  81. res.push(this.table[(c2 & 0x0F) << 2]);
  82. res.push('=');
  83. break;
  84. }
  85. var c3 = utf8.charCodeAt(i++);
  86. res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
  87. res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
  88. res.push(this.table[c3 & 0x3F]);
  89. }
  90. return res.join('');
  91. },
  92. decode: function (str) {
  93. !str ? '' : 1;
  94. var len = str.length,
  95. i = 0,
  96. res = [];
  97. while (i < len) {
  98. code1 = this.table.indexOf(str.charAt(i++));
  99. code2 = this.table.indexOf(str.charAt(i++));
  100. code3 = this.table.indexOf(str.charAt(i++));
  101. code4 = this.table.indexOf(str.charAt(i++));
  102. c1 = (code1 << 2) | (code2 >> 4);
  103. c2 = ((code2 & 0xF) << 4) | (code3 >> 2);
  104. c3 = ((code3 & 0x3) << 6) | code4;
  105. res.push(String.fromCharCode(c1));
  106. code3 != 64 ? res.push(String.fromCharCode(c2)) : 1;
  107. code4 != 64 ? res.push(String.fromCharCode(c3)) : 1;
  108. }
  109. return this.UTF8ToUTF16(res.join(''));
  110. }
  111. };
  112. var b64 = Base64.encode('KyleThanas,Base64,简书');
  113. console.log(b64);
  114. console.log(Base64.decode(b64));

其他的转换

JSON.parse(escape2Html(data));

  1. function escape2Html(str) {
  2. var arrEntities = {
  3. 'lt': '<',
  4. 'gt': '>',
  5. 'nbsp': ' ',
  6. 'amp': '&',
  7. 'quot': '"'
  8. };
  9. return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function(all, t) {
  10. return arrEntities[t];
  11. });
  12. }

更多编码、算法

sha1
https://kylethanas.coding.me/demo/js/sha1.js
var sha = hex_sha1(navigator.userAgent)
md5
http://kylethanas.coding.me/demo/js/md5.js
var hash = hex_md5(navigator.userAgent);

我是Kylethanas🐏