开发者

Char to Hex in javascript

开发者 https://www.devze.com 2023-02-28 13:09 出处:网络
Could anyone guide me on how to convert char to hex in javascript? For example: \"入力されたデータは範囲外です。\" 开发者_开发技巧to

Could anyone guide me on how to convert char to hex in javascript?

For example:

"入力されたデータは範囲外です。"

开发者_开发技巧to

"\u5165\u529B\u3055\u308C\u305F\u30C7\u30FC\u30BF\u306F\u7BC4\u56F2\u5916\u3067\u3059\u3002"

This site does it

However I can not figure it out.

Any suggestion.

Thanks, Sarbbottam


You can loop through the characters and use the charCodeAt function to get their UTF-16 values, then constructing a string with them.

Here's some code I constructed that is much better than the code on the site you've linked, and should be easier to understand:

function string_as_unicode_escape(input) {
    function pad_four(input) {
        var l = input.length;
        if (l == 0) return '0000';
        if (l == 1) return '000' + input;
        if (l == 2) return '00' + input;
        if (l == 3) return '0' + input;
        return input;
    }
    var output = '';
    for (var i = 0, l = input.length; i < l; i++)
        output += '\\u' + pad_four(input.charCodeAt(i).toString(16));
    return output;
}

Let's break it down.

  1. string_as_unicode_escape takes one argument, input, which is a string.
  2. pad_four is an internal function that does one thing; it pads strings with leading '0' characters until the length is at least four characters long.
  3. Start off by defining output as an empty string.
  4. For each character in the string, append \u to the output string. Take the UTF-16 value of the character with input.charCodeAt(i), then convert it to a hexadecimal string with .toString(16), then pad it with leading zeros, then append the result to the output string.
  5. Return the output string.

As Tim Down commented, we can also add 0x10000 to the charCodeAt value and then .slice(1) the string resulting from calling .toString(16), to achieve the padding effect.


function string_as_unicode_escape(str){
    return str.split("").map(function(s){
        return "\\u"+("0000" + s.charCodeAt(0).toString(16)).slice(-4);
    }).join("");
}


var hex=new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');

    function stringEncode()
    {
        var x=document.getElementById("from_text");


        var preescape="" + x.value;
        var escaped="";

        var i=0;
        for(i=0;i<preescape.length;i++)
        {
            escaped=escaped+encodeChar(preescape.charAt(i));
        }

        //x=document.getElementById("to_text");

                    x.value=escaped;
        //alert("Codigo: "+escapeHtml(escaped));
        //document.getElementById('string_example').innerHTML="<b>String example with text</b><br/><br/>String s=\""+escapeHtml(escaped)+"\";<br/><br/>";
    }


    function escapeHtml(unsafe) {
       return unsafe
                .replace(/&/g, "&amp;")
                .replace(/</g, "&lt;")
                .replace(/>/g, "&gt;")
                .replace(/"/g, "&quot;")
                .replace(/'/g, "&#039;");
            }

            function encodeChar(original)
            {
                    var found=true;
                    var thecharchar=original.charAt(0);
                    var thechar=original.charCodeAt(0);
                    switch(thecharchar) {
                                    case '\n': return "\\n"; break; //newline
                                    case '\r': return "\\r"; break; //Carriage return
                                    case '\'': return "\\'"; break;
                                    case '"': return "\\\""; break;
                                    case '\\': return "\\\\"; break;
                                    case '\t': return "\\t"; break;
                                    case '\b': return "\\b"; break;
                                    case '\f': return "\\f"; break;

                                    default:
                                            found=false;
                                            break;
                            }
                            if(!found)
                            {
                                    if(thechar>127) {
                                            var c=thechar;
                                            var a4=c%16;
                                            c=Math.floor(c/16); 
                                            var a3=c%16;
                                            c=Math.floor(c/16);
                                            var a2=c%16;
                                            c=Math.floor(c/16);
                                            var a1=c%16;
                                    //  alert(a1);
                                            return "\\u"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+"";        
                                    }
                                    else
                                    {
                                            return original;
                                    }
                            }


            }

//------------------------ lo llamarias con


You can just use ordinary replace() for this.

'! \u0100 力 
0

精彩评论

暂无评论...
验证码 换一张
取 消