Char to Hex in javascript

前端 未结 4 1924
鱼传尺愫
鱼传尺愫 2020-12-05 05:09

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

For example:

\"入力されたデータは範囲外です。\"
to
\"\\u5165\\u529B\\u3055\\

相关标签:
4条回答
  • 2020-12-05 05:50

    You can just use ordinary replace() for this.

    '! \u0100 力                                                                     
    0 讨论(0)
  • 2020-12-05 05:57
    function string_as_unicode_escape(str){
        return str.split("").map(function(s){
            return "\\u"+("0000" + s.charCodeAt(0).toString(16)).slice(-4);
        }).join("");
    }
    
    0 讨论(0)
  • 2020-12-05 06:03

    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.

    0 讨论(0)
  • 2020-12-05 06:03

    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

    0 讨论(0)
提交回复
热议问题