diff --git "a/\346\240\270\345\277\203\344\273\267\345\200\274\350\247\202\347\274\226\347\240\201\345\231\250\357\274\214\345\210\206\344\272\253\347\275\221\345\235\200\345\210\251\345\231\250.md." "b/\346\240\270\345\277\203\344\273\267\345\200\274\350\247\202\347\274\226\347\240\201\345\231\250\357\274\214\345\210\206\344\272\253\347\275\221\345\235\200\345\210\251\345\231\250.md." new file mode 100644 index 0000000000000000000000000000000000000000..433f96058dd7792c084ce2ec2cbf8ee17f01f015 --- /dev/null +++ "b/\346\240\270\345\277\203\344\273\267\345\200\274\350\247\202\347\274\226\347\240\201\345\231\250\357\274\214\345\210\206\344\272\253\347\275\221\345\235\200\345\210\251\345\231\250.md." @@ -0,0 +1,121 @@ +JS 实现的核心价值观编码器,可以悄眯眯分享网站咯(迷之微笑) + +访问地址:[在线编码解码](http://winshu.iask.in/greeting/corevaluesencoder) + +Chrome 插件地址:http://gam.ys168.com + +核心源码如下: + +```javascript +// 核心价值观字典 +const corevalues = '富强民主文明和谐自由平等公正法治爱国敬业诚信友善'; + +// 实现编码的函数 +function encode(codes) { + let result = ''; + let bytes = str2utf8(codes); + for (let i in bytes) { + let hex = bytes[i].toString(16); + for (let j in hex) { + let vInt = parseInt(hex[j], 16); + if (vInt < 10) { + result += corevalues.substr(2 * vInt, 2); + } else { + let flag = randomBoolean(); + let a = flag ? 10 : 11; + let b = flag ? 10 : 6; + result += corevalues.substr(2 * a, 2); + result += corevalues.substr(2 * (vInt - b), 2); + } + } + } + return result; +} +// 实现解码的函数 +function decode(values) { + if (values.length % 2 !== 0) { + return null; + } + let bits = []; + let preIndex = -1; + + for (let i = 0; i < values.length / 2; i++) { + let ch = values.substr(2 * i, 2); + let index = corevalues.indexOf(ch) / 2; + if (index < 0) { + return null; + } + + if (preIndex >= 10) { + bits.push(index + (preIndex === 10 ? 10 : 6)); + } else if (index < 10) { + bits.push(index); + } + preIndex = index; // 存储之前标记位 + } + + let bytes = []; + for (let i = 0; i < bits.length; i += 2) { + let a = bits[i].toString(16); + let b = bits[i + 1].toString(16); + bytes.push(parseInt(a + b, 16)); + } + return utf82str(bytes); +} +// 字符串转字节数组 +function str2utf8(str) { + let c; + let bytes = []; + for (let i in str) { + c = str.charCodeAt(i); + if (c >= 0x010000 && c <= 0x10FFFF) { + bytes.push(((c >> 18) & 0x07) | 0xF0); + bytes.push(((c >> 12) & 0x3F) | 0x80); + bytes.push(((c >> 6) & 0x3F) | 0x80); + bytes.push((c & 0x3F) | 0x80); + } else if (c >= 0x000800 && c <= 0x00FFFF) { + bytes.push(((c >> 12) & 0x0F) | 0xE0); + bytes.push(((c >> 6) & 0x3F) | 0x80); + bytes.push((c & 0x3F) | 0x80); + } else if (c >= 0x000080 && c <= 0x0007FF) { + bytes.push(((c >> 6) & 0x1F) | 0xC0); + bytes.push((c & 0x3F) | 0x80); + } else { + bytes.push(c & 0xFF); + } + } + return bytes; +} + +function utf82str(bytes) { + let str = ''; + if (!Array.isArray(bytes)) { + return str; + } + _arr = bytes; + for (let i = 0; i < _arr.length; i++) { + let one = _arr[i].toString(2); + let v = one.match(/^1+?(?=0)/); + if (v && one.length == 8) { + let bytesLength = v[0].length; + let store = _arr[i].toString(2).slice(7 - bytesLength); + for (let st = 1; st < bytesLength; st++) { + store += _arr[st + i].toString(2).slice(2); + } + str += String.fromCharCode(parseInt(store, 2)); + i += bytesLength - 1; + } else { + str += String.fromCharCode(_arr[i]); + } + } + return str; +} + +function randomBoolean() { + return Math.random() >= 0.5 ? true : false; +} +``` + +分享一个大家经常去的地址: + +公正爱国法治自由法治自由法治富强法治和谐和谐友善自由文明诚信平等文明诚信平等法治法治法治法治法治法治文明诚信自由公正友善敬业法治和谐公正和谐公正爱国公正敬业公正诚信自由公正民主文明友善爱国公正友善爱国公正平等法治自由 \ No newline at end of file