Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

emoji 存入数据库的前端处理方法(The front-end processing method for storing emoji in database) #79

Open
yyccQQu opened this issue Jan 19, 2021 · 1 comment

Comments

@yyccQQu
Copy link

yyccQQu commented Jan 19, 2021

//把utf16的emoji表情字符进行转码成八进制的字符
//Transcode emoji characters of utf16 into octal characters
export function utf16toEntities(str) {
    var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则  
    return str.replace(patt, function(char) {
        var H, L, code;
        if (char.length === 2) {
            H = char.charCodeAt(0); // 取出高位  
            L = char.charCodeAt(1); // 取出低位  
            code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法  
            return "&#" + code + ";";
        } else {
            return char;
        }
    });
}

//将编码后的八进制的emoji表情重新解码成十六进制的表情字符
//Re-decode the encoded octal emoji expressions into hexadecimal expression characters
export function entitiesToUtf16(str) {
    return str.replace(/&#(\d+);/g, function(match, dec) {
        let H = Math.floor((dec - 0x10000) / 0x400) + 0xD800;
        let L = Math.floor(dec - 0x10000) % 0x400 + 0xDC00;
        return String.fromCharCode(H, L);
    });
}

var a = utf16toEntities("🤓2333😎");
console.log("a: ", a);


var b = entitiesToUtf16(a)
console.log("b: ",b)

var c = entitiesToUtf16("🤓2333😎");
console.log("c: ", c);
@joaoeudes7
Copy link
Owner

I didn't understand about the problem, please be clear about the problem and in English

@yyccQQu yyccQQu changed the title emoji 存入数据库的前端处理方法 emoji 存入数据库的前端处理方法(The front-end processing method for storing emoji in database) May 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants