You need to sign in or sign up before continuing.

Mon May 8 03:02:00 UTC 2023 inscode

上级 ca207a2f
console.log("欢迎来到 InsCode"); console.log("欢迎来到 InsCode");
\ No newline at end of file enc = ["o", "ο", "о", ""]
console.log(encodeURI('o-ο-о-ᴏ'))
var dec = {
'o': 0,
'ο': 1,
'о': 2,
'': 3
};
var url = "https://www.baidu.com"
// 获取utf8数组
// let unversioned = toUTF8Array(url)
// // 转换为base 4字符串
// // padstart非常重要!否则会丢失前导0
// .map(n => n.toString(4).padStart(4, "0"))
// // 转换为字符数组
// .join("").split("")
// // 映射到o的不同形式
// .map(x => enc[parseInt(x)])
// // 连接成单个字符串
// .join("")
let unversioned = toUTF8Array(url)
// 转换为base 4字符串
let b4str = unversioned.map(n => n.toString(4).padStart(4, "0"))
console.log("转换为base 4字符串:\n", b4str)
// 转换为字符数组
let str = b4str.join("").split("")
console.log("转换为字符数组:\n", str)
// 映射到o的不同形式
let ooo = str.map(x => enc[parseInt(x)]).join("")
console.log("映射到o的不同形式:\n", ooo)
function getUrl(str) {
// 获取url的base 4字符串表示
let b4str = str.split("").map(x => dec[x]).join("")
let utf8arr = []
// 每次解析4个字符
// 记住添加前导0的填充
for (let i = 0; i < b4str.length; i += 4)
utf8arr.push(parseInt(b4str.substring(i, i + 4), 4))
// 返回解码后的字符串
return Utf8ArrayToStr(utf8arr)
}
function toUTF8Array(str) {
var utf8 = [];
for (var i = 0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f));
}
else {
i++;
charcode = ((charcode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)
utf8.push(0xf0 | (charcode >> 18),
0x80 | ((charcode >> 12) & 0x3f),
0x80 | ((charcode >> 6) & 0x3f),
0x80 | (charcode & 0x3f));
}
}
console.log(utf8, 'utf8');
return utf8;
}
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册