How to convert string between Unicode and Ascii with JavaScript Back

Ascii to Unicode

function A2U(str) {
    var reserved = '';

    for (var i = 0; i < str.length; i++) {
        reserved += '&#' + str.charCodeAt(i) + ';';
    }

    return reserved;
}

Unicode to Ascii

function U2A(str) {
    var reserved = '';
    var code = str.match(/&#(d+);/g);

    if (code === null) {
        return str;
    }

    for (var i = 0; i < code.length; i++) {
        reserved += String.fromCharCode(code[i].replace(/[&#;]/g, ''));
    }

    return reserved;
}
Empty Comments
Sign in GitHub

As the plugin is integrated with a code management system like GitLab or GitHub, you may have to auth with your account before leaving comments around this article.

Notice: This plugin has used Cookie to store your token with an expiration.