javascriptunicodecharacterunicode-literals

How to print literal unicode string in Javascript?


I have an array containing strings with special unicode characters:

var a = [
    ["a", 33],  
    ["h\u016B", 44],
    ["s\u00EF", 51],
    ...
];

When I loop over this array:

for (i=0;i<a.length;i++) {
    document.write(a[i][0] + "<br />");
}

It prints characters with accents:

a
hù
sô
...

and I want:

a
h\u016B
s\u00EF
...

How can I achieve this in Javascript?


Solution

  • Something like this?

    /* Creates a uppercase hex number with at least length digits from a given number */
    function fixedHex(number, length){
        var str = number.toString(16).toUpperCase();
        while(str.length < length)
            str = "0" + str;
        return str;
    }
    
    /* Creates a unicode literal based on the string */    
    function unicodeLiteral(str){
        var i;
        var result = "";
        for( i = 0; i < str.length; ++i){
            /* You should probably replace this by an isASCII test */
            if(str.charCodeAt(i) > 126 || str.charCodeAt(i) < 32)
                result += "\\u" + fixedHex(str.charCodeAt(i),4);
            else
                result += str[i];
        }
    
        return result;
    }
    
    var a = [
        ["a", 33],  
        ["h\u016B", 44],
        ["s\u00EF", 51]
    ];
    
    var i;
    for (i=0;i<a.length;i++) {
        document.write(unicodeLiteral(a[i][0]) + "<br />");
    }
    

    Result

    a
    h\u016B
    s\u00EF

    JSFiddle