Encode String to HEX
<p>i have my function to convert string to hex:</p>

<pre><code>function encode(str){
    str = encodeURIComponent(str).split('%').join('');
    return str.toLowerCase();
}
</code></pre>

<p>example:</p>

<p><code>守护村子</code></p>

<p><code>alert(encode('守护村子'));</code></p>

<p>the output would be:</p>

<p><code>e5ae88e68aa4e69d91e5ad90</code></p>

<p>It works on Chinese characters. But when i do it with English letters</p>

<p><code>alert(encode('Hello World'))</code>;</p>

<p>it outputs:</p>

<p><code>hello20world</code></p>

<p>I have tried this for converting string to hex:</p>

<pre><code>function String2Hex(tmp) {
    var str = '';
    for(var i = 0; i &lt; tmp.length; i++) {
        str += tmp[i].charCodeAt(0).toString(16);
    }
    return str;
}
</code></pre>

<p>then tried it on the  Chinese characters above, but it outputs the UTF-8 HEX:</p>

<p><code>5b8862a467515b50</code></p>

<p>not the ANSI Hex:</p>

<p><code>e5ae88e68aa4e69d91e5ad90</code></p>

<p>I also have searched converting UFT8 to ANSI but no luck.
Anyone could help me? Thanks!</p>