UTF-8 to UTF-16LE Javascript - javascript

I need to convert an utf-8 string to utf-16LE in javascript like the iconv() php function.
Ie:
iconv("UTF-8", "UTF-16LE", $string);
The output should be like this:
49 00 6e 00 64 00 65 00 78 00
I found this func to decode UTF-16LE and it's works fine but i don't know how to do the same to encode.
function decodeUTF16LE( binaryStr ) {
var cp = [];
for( var i = 0; i < binaryStr.length; i+=2) {
cp.push(
binaryStr.charCodeAt(i) |
( binaryStr.charCodeAt(i+1) << 8 )
);
}
return String.fromCharCode.apply( String, cp );
}
The conclusion is to create a binary file that can be downloaded.
The code:
function download(filename, text) {
var a = window.document.createElement('a');
var byteArray = new Uint8Array(text.length);
for (var i = 0; i < text.length; i++) {
byteArray[i] = text.charCodeAt(i) & 0xff;
}
a.href = window.URL.createObjectURL(new Blob([byteArray.buffer], {'type': 'application/type'}));
a.download = filename;
// Append anchor to body.
document.body.appendChild(a);
a.click();
// Remove anchor from body
document.body.removeChild(a);
}

This should do it:
var byteArray = new Uint8Array(text.length * 2);
for (var i = 0; i < text.length; i++) {
byteArray[i*2] = text.charCodeAt(i) // & 0xff;
byteArray[i*2+1] = text.charCodeAt(i) >> 8 // & 0xff;
}
It's the inverse of your decodeUTF16LE function. Notice that neither works with code points outside of the BMP.

Thanks a lot Bergi, this works perfectly combining to standard utf8 to utf16 encode function:
function encodeUTF16LE(str) {
var out, i, len, c;
var char2, char3;
out = "";
len = str.length;
i = 0;
while(i < len) {
c = str.charCodeAt(i++);
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += str.charAt(i-1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
out += str.charAt(i-1);
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
}
}
var byteArray = new Uint8Array(out.length * 2);
for (var i = 0; i < out.length; i++) {
byteArray[i*2] = out.charCodeAt(i); // & 0xff;
byteArray[i*2+1] = out.charCodeAt(i) >> 8; // & 0xff;
}
return String.fromCharCode.apply( String, byteArray );
}

Related

Why C# side pass 1.3M bytes/string to js. js recieve 2.0M bytes but only 1.3M string?

today I meet a weird issue....
I don't know why I recieve 2M bytes and transfer to 1.3M string. And in C# side,I checked I only send 1.3M string data.
I pass a string from C# MVC to js ajax response.
in C# side, it encoding with UTF16. I change it to bytes so that I can check it byte size. It show 1.3M bytes.
In js side, responseType = arraybuffer.
And the arraybuffer length = 2016806.
I change arraybuffer to uint8Array(show length=2016806) then transform to js string
string length = 689060(680K chars)
then I use sizeof function(custom function)to check the byte size.
it show the 1.3M......
What's happen in this process?.
I sincerely appreciate for your reply!
Js Side:
xhr.responseType="arraybuffer"
xhr.send(JSON.stringify({ sessionId: sessionId, pageId: pageId }));
xhr.addEventListener("load", function (ev) {
var arrayBuffer = xhr.response;
var byteArray = new Uint8Array(arrayBuffer);
var reText = Utf8ArrayToStr(byteArray);
var size = sizeof(reText,"utf-16");
In Chrome Dev Tool Watch
reText.length:689060
byteArray.length:2016806
size:1378120
byteArray=Uint8Array(2016806)
arrayBuffer.byteLength:2016806
C# side:
[HttpPost]
public string GetReplayData(string sessionId, string pageId)
{
var parameters = JsonConvert.SerializeObject(new { sessionId, pageId });
var result = _provider.Get(parameters);
return JsonConvert.SerializeObject(result);
}
C# in VS watch
Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(result)).Length 1378120
JsonConvert.SerializeObject(result[0].RawData).Length 688875
JS sizeof function:
var sizeof = function(str, charset){
var total = 0,
charCode,
i,
len;
charset = charset ? charset.toLowerCase() : '';
if(charset === 'utf-16' || charset === 'utf16'){
for(i = 0, len = str.length; i < len; i++){
charCode = str.charCodeAt(i);
if(charCode <= 0xffff){
total += 2;
}else{
total += 4;
}
}
}else{
for(i = 0, len = str.length; i < len; i++){
charCode = str.charCodeAt(i);
if(charCode <= 0x007f) {
total += 1;
}else if(charCode <= 0x07ff){
total += 2;
}else if(charCode <= 0xffff){
total += 3;
}else{
total += 4;
}
}
}
return total;
}
JS UTF8ArrayToStr:
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}

FireFox Extenstion - Using Components - Hex Decoding

I am working on a FireFox Extension and want to know what are these codes and what do they do :
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
var wm = Components.classes["#mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
var windows = wm.getEnumerator("navigator:browser");
global_win = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
var my_integer = check_me_1() + check_me_2();
alert(my_integer);
function check_me_1() {
try {
var _0x2cb6 = ["\x4E\x74\x74", "\x77\x69\x64\x74\x68", "\x73\x63\x72\x65\x65\x6E", "\x2E", "\x68\x65\x69\x67\x68\x74", "\x0A", "\x72\x65\x70\x6C\x61\x63\x65", "", "\x6C\x65\x6E\x67\x74\x68", "\x63\x68\x61\x72\x43\x6F\x64\x65\x41\x74", "\x66\x72\x6F\x6D\x43\x68\x61\x72\x43\x6F\x64\x65", "\x30\x30\x30\x30\x30\x30\x30\x30\x20\x37\x37\x30\x37\x33\x30\x39\x36\x20\x45\x45\x30\x45\x36\x31\x32\x43\x20\x39\x39\x30\x39\x35\x31\x42\x41\x20\x30\x37\x36\x44\x43\x34\x31\x39\x20\x37\x30\x36\x41\x46\x34\x38\x46\x20\x45\x39\x36\x33\x41\x35\x33\x35\x20\x39\x45\x36\x34\x39\x35\x41\x33\x20\x30\x45\x44\x42\x38\x38\x33\x32\x20\x37\x39\x44\x43\x42\x38\x41\x34\x20\x45\x30\x44\x35\x45\x39\x31\x45\x20\x39\x37\x44\x32\x44\x39\x38\x38\x20\x30\x39\x42\x36\x34\x43\x32\x42\x20\x37\x45\x42\x31\x37\x43\x42\x44\x20\x45\x37\x42\x38\x32\x44\x30\x37\x20\x39\x30\x42\x46\x31\x44\x39\x31\x20\x31\x44\x42\x37\x31\x30\x36\x34\x20\x36\x41\x42\x30\x32\x30\x46\x32\x20\x46\x33\x42\x39\x37\x31\x34\x38\x20\x38\x34\x42\x45\x34\x31\x44\x45\x20\x31\x41\x44\x41\x44\x34\x37\x44\x20\x36\x44\x44\x44\x45\x34\x45\x42\x20\x46\x34\x44\x34\x42\x35\x35\x31\x20\x38\x33\x44\x33\x38\x35\x43\x37\x20\x31\x33\x36\x43\x39\x38\x35\x36\x20\x36\x34\x36\x42\x41\x38\x43\x30\x20\x46\x44\x36\x32\x46\x39\x37\x41\x20\x38\x41\x36\x35\x43\x39\x45\x43\x20\x31\x34\x30\x31\x35\x43\x34\x46\x20\x36\x33\x30\x36\x36\x43\x44\x39\x20\x46\x41\x30\x46\x33\x44\x36\x33\x20\x38\x44\x30\x38\x30\x44\x46\x35\x20\x33\x42\x36\x45\x32\x30\x43\x38\x20\x34\x43\x36\x39\x31\x30\x35\x45\x20\x44\x35\x36\x30\x34\x31\x45\x34\x20\x41\x32\x36\x37\x37\x31\x37\x32\x20\x33\x43\x30\x33\x45\x34\x44\x31\x20\x34\x42\x30\x34\x44\x34\x34\x37\x20\x44\x32\x30\x44\x38\x35\x46\x44\x20\x41\x35\x30\x41\x42\x35\x36\x42\x20\x33\x35\x42\x35\x41\x38\x46\x41\x20\x34\x32\x42\x32\x39\x38\x36\x43\x20\x44\x42\x42\x42\x43\x39\x44\x36\x20\x41\x43\x42\x43\x46\x39\x34\x30\x20\x33\x32\x44\x38\x36\x43\x45\x33\x20\x34\x35\x44\x46\x35\x43\x37\x35\x20\x44\x43\x44\x36\x30\x44\x43\x46\x20\x41\x42\x44\x31\x33\x44\x35\x39\x20\x32\x36\x44\x39\x33\x30\x41\x43\x20\x35\x31\x44\x45\x30\x30\x33\x41\x20\x43\x38\x44\x37\x35\x31\x38\x30\x20\x42\x46\x44\x30\x36\x31\x31\x36\x20\x32\x31\x42\x34\x46\x34\x42\x35\x20\x35\x36\x42\x33\x43\x34\x32\x33\x20\x43\x46\x42\x41\x39\x35\x39\x39\x20\x42\x38\x42\x44\x41\x35\x30\x46\x20\x32\x38\x30\x32\x42\x38\x39\x45\x20\x35\x46\x30\x35\x38\x38\x30\x38\x20\x43\x36\x30\x43\x44\x39\x42\x32\x20\x42\x31\x30\x42\x45\x39\x32\x34\x20\x32\x46\x36\x46\x37\x43\x38\x37\x20\x35\x38\x36\x38\x34\x43\x31\x31\x20\x43\x31\x36\x31\x31\x44\x41\x42\x20\x42\x36\x36\x36\x32\x44\x33\x44\x20\x37\x36\x44\x43\x34\x31\x39\x30\x20\x30\x31\x44\x42\x37\x31\x30\x36\x20\x39\x38\x44\x32\x32\x30\x42\x43\x20\x45\x46\x44\x35\x31\x30\x32\x41\x20\x37\x31\x42\x31\x38\x35\x38\x39\x20\x30\x36\x42\x36\x42\x35\x31\x46\x20\x39\x46\x42\x46\x45\x34\x41\x35\x20\x45\x38\x42\x38\x44\x34\x33\x33\x20\x37\x38\x30\x37\x43\x39\x41\x32\x20\x30\x46\x30\x30\x46\x39\x33\x34\x20\x39\x36\x30\x39\x41\x38\x38\x45\x20\x45\x31\x30\x45\x39\x38\x31\x38\x20\x37\x46\x36\x41\x30\x44\x42\x42\x20\x30\x38\x36\x44\x33\x44\x32\x44\x20\x39\x31\x36\x34\x36\x43\x39\x37\x20\x45\x36\x36\x33\x35\x43\x30\x31\x20\x36\x42\x36\x42\x35\x31\x46\x34\x20\x31\x43\x36\x43\x36\x31\x36\x32\x20\x38\x35\x36\x35\x33\x30\x44\x38\x20\x46\x32\x36\x32\x30\x30\x34\x45\x20\x36\x43\x30\x36\x39\x35\x45\x44\x20\x31\x42\x30\x31\x41\x35\x37\x42\x20\x38\x32\x30\x38\x46\x34\x43\x31\x20\x46\x35\x30\x46\x43\x34\x35\x37\x20\x36\x35\x42\x30\x44\x39\x43\x36\x20\x31\x32\x42\x37\x45\x39\x35\x30\x20\x38\x42\x42\x45\x42\x38\x45\x41\x20\x46\x43\x42\x39\x38\x38\x37\x43\x20\x36\x32\x44\x44\x31\x44\x44\x46\x20\x31\x35\x44\x41\x32\x44\x34\x39\x20\x38\x43\x44\x33\x37\x43\x46\x33\x20\x46\x42\x44\x34\x34\x43\x36\x35\x20\x34\x44\x42\x32\x36\x31\x35\x38\x20\x33\x41\x42\x35\x35\x31\x43\x45\x20\x41\x33\x42\x43\x30\x30\x37\x34\x20\x44\x34\x42\x42\x33\x30\x45\x32\x20\x34\x41\x44\x46\x41\x35\x34\x31\x20\x33\x44\x44\x38\x39\x35\x44\x37\x20\x41\x34\x44\x31\x43\x34\x36\x44\x20\x44\x33\x44\x36\x46\x34\x46\x42\x20\x34\x33\x36\x39\x45\x39\x36\x41\x20\x33\x34\x36\x45\x44\x39\x46\x43\x20\x41\x44\x36\x37\x38\x38\x34\x36\x20\x44\x41\x36\x30\x42\x38\x44\x30\x20\x34\x34\x30\x34\x32\x44\x37\x33\x20\x33\x33\x30\x33\x31\x44\x45\x35\x20\x41\x41\x30\x41\x34\x43\x35\x46\x20\x44\x44\x30\x44\x37\x43\x43\x39\x20\x35\x30\x30\x35\x37\x31\x33\x43\x20\x32\x37\x30\x32\x34\x31\x41\x41\x20\x42\x45\x30\x42\x31\x30\x31\x30\x20\x43\x39\x30\x43\x32\x30\x38\x36\x20\x35\x37\x36\x38\x42\x35\x32\x35\x20\x32\x30\x36\x46\x38\x35\x42\x33\x20\x42\x39\x36\x36\x44\x34\x30\x39\x20\x43\x45\x36\x31\x45\x34\x39\x46\x20\x35\x45\x44\x45\x46\x39\x30\x45\x20\x32\x39\x44\x39\x43\x39\x39\x38\x20\x42\x30\x44\x30\x39\x38\x32\x32\x20\x43\x37\x44\x37\x41\x38\x42\x34\x20\x35\x39\x42\x33\x33\x44\x31\x37\x20\x32\x45\x42\x34\x30\x44\x38\x31\x20\x42\x37\x42\x44\x35\x43\x33\x42\x20\x43\x30\x42\x41\x36\x43\x41\x44\x20\x45\x44\x42\x38\x38\x33\x32\x30\x20\x39\x41\x42\x46\x42\x33\x42\x36\x20\x30\x33\x42\x36\x45\x32\x30\x43\x20\x37\x34\x42\x31\x44\x32\x39\x41\x20\x45\x41\x44\x35\x34\x37\x33\x39\x20\x39\x44\x44\x32\x37\x37\x41\x46\x20\x30\x34\x44\x42\x32\x36\x31\x35\x20\x37\x33\x44\x43\x31\x36\x38\x33\x20\x45\x33\x36\x33\x30\x42\x31\x32\x20\x39\x34\x36\x34\x33\x42\x38\x34\x20\x30\x44\x36\x44\x36\x41\x33\x45\x20\x37\x41\x36\x41\x35\x41\x41\x38\x20\x45\x34\x30\x45\x43\x46\x30\x42\x20\x39\x33\x30\x39\x46\x46\x39\x44\x20\x30\x41\x30\x30\x41\x45\x32\x37\x20\x37\x44\x30\x37\x39\x45\x42\x31\x20\x46\x30\x30\x46\x39\x33\x34\x34\x20\x38\x37\x30\x38\x41\x33\x44\x32\x20\x31\x45\x30\x31\x46\x32\x36\x38\x20\x36\x39\x30\x36\x43\x32\x46\x45\x20\x46\x37\x36\x32\x35\x37\x35\x44\x20\x38\x30\x36\x35\x36\x37\x43\x42\x20\x31\x39\x36\x43\x33\x36\x37\x31\x20\x36\x45\x36\x42\x30\x36\x45\x37\x20\x46\x45\x44\x34\x31\x42\x37\x36\x20\x38\x39\x44\x33\x32\x42\x45\x30\x20\x31\x30\x44\x41\x37\x41\x35\x41\x20\x36\x37\x44\x44\x34\x41\x43\x43\x20\x46\x39\x42\x39\x44\x46\x36\x46\x20\x38\x45\x42\x45\x45\x46\x46\x39\x20\x31\x37\x42\x37\x42\x45\x34\x33\x20\x36\x30\x42\x30\x38\x45\x44\x35\x20\x44\x36\x44\x36\x41\x33\x45\x38\x20\x41\x31\x44\x31\x39\x33\x37\x45\x20\x33\x38\x44\x38\x43\x32\x43\x34\x20\x34\x46\x44\x46\x46\x32\x35\x32\x20\x44\x31\x42\x42\x36\x37\x46\x31\x20\x41\x36\x42\x43\x35\x37\x36\x37\x20\x33\x46\x42\x35\x30\x36\x44\x44\x20\x34\x38\x42\x32\x33\x36\x34\x42\x20\x44\x38\x30\x44\x32\x42\x44\x41\x20\x41\x46\x30\x41\x31\x42\x34\x43\x20\x33\x36\x30\x33\x34\x41\x46\x36\x20\x34\x31\x30\x34\x37\x41\x36\x30\x20\x44\x46\x36\x30\x45\x46\x43\x33\x20\x41\x38\x36\x37\x44\x46\x35\x35\x20\x33\x31\x36\x45\x38\x45\x45\x46\x20\x34\x36\x36\x39\x42\x45\x37\x39\x20\x43\x42\x36\x31\x42\x33\x38\x43\x20\x42\x43\x36\x36\x38\x33\x31\x41\x20\x32\x35\x36\x46\x44\x32\x41\x30\x20\x35\x32\x36\x38\x45\x32\x33\x36\x20\x43\x43\x30\x43\x37\x37\x39\x35\x20\x42\x42\x30\x42\x34\x37\x30\x33\x20\x32\x32\x30\x32\x31\x36\x42\x39\x20\x35\x35\x30\x35\x32\x36\x32\x46\x20\x43\x35\x42\x41\x33\x42\x42\x45\x20\x42\x32\x42\x44\x30\x42\x32\x38\x20\x32\x42\x42\x34\x35\x41\x39\x32\x20\x35\x43\x42\x33\x36\x41\x30\x34\x20\x43\x32\x44\x37\x46\x46\x41\x37\x20\x42\x35\x44\x30\x43\x46\x33\x31\x20\x32\x43\x44\x39\x39\x45\x38\x42\x20\x35\x42\x44\x45\x41\x45\x31\x44\x20\x39\x42\x36\x34\x43\x32\x42\x30\x20\x45\x43\x36\x33\x46\x32\x32\x36\x20\x37\x35\x36\x41\x41\x33\x39\x43\x20\x30\x32\x36\x44\x39\x33\x30\x41\x20\x39\x43\x30\x39\x30\x36\x41\x39\x20\x45\x42\x30\x45\x33\x36\x33\x46\x20\x37\x32\x30\x37\x36\x37\x38\x35\x20\x30\x35\x30\x30\x35\x37\x31\x33\x20\x39\x35\x42\x46\x34\x41\x38\x32\x20\x45\x32\x42\x38\x37\x41\x31\x34\x20\x37\x42\x42\x31\x32\x42\x41\x45\x20\x30\x43\x42\x36\x31\x42\x33\x38\x20\x39\x32\x44\x32\x38\x45\x39\x42\x20\x45\x35\x44\x35\x42\x45\x30\x44\x20\x37\x43\x44\x43\x45\x46\x42\x37\x20\x30\x42\x44\x42\x44\x46\x32\x31\x20\x38\x36\x44\x33\x44\x32\x44\x34\x20\x46\x31\x44\x34\x45\x32\x34\x32\x20\x36\x38\x44\x44\x42\x33\x46\x38\x20\x31\x46\x44\x41\x38\x33\x36\x45\x20\x38\x31\x42\x45\x31\x36\x43\x44\x20\x46\x36\x42\x39\x32\x36\x35\x42\x20\x36\x46\x42\x30\x37\x37\x45\x31\x20\x31\x38\x42\x37\x34\x37\x37\x37\x20\x38\x38\x30\x38\x35\x41\x45\x36\x20\x46\x46\x30\x46\x36\x41\x37\x30\x20\x36\x36\x30\x36\x33\x42\x43\x41\x20\x31\x31\x30\x31\x30\x42\x35\x43\x20\x38\x46\x36\x35\x39\x45\x46\x46\x20\x46\x38\x36\x32\x41\x45\x36\x39\x20\x36\x31\x36\x42\x46\x46\x44\x33\x20\x31\x36\x36\x43\x43\x46\x34\x35\x20\x41\x30\x30\x41\x45\x32\x37\x38\x20\x44\x37\x30\x44\x44\x32\x45\x45\x20\x34\x45\x30\x34\x38\x33\x35\x34\x20\x33\x39\x30\x33\x42\x33\x43\x32\x20\x41\x37\x36\x37\x32\x36\x36\x31\x20\x44\x30\x36\x30\x31\x36\x46\x37\x20\x34\x39\x36\x39\x34\x37\x34\x44\x20\x33\x45\x36\x45\x37\x37\x44\x42\x20\x41\x45\x44\x31\x36\x41\x34\x41\x20\x44\x39\x44\x36\x35\x41\x44\x43\x20\x34\x30\x44\x46\x30\x42\x36\x36\x20\x33\x37\x44\x38\x33\x42\x46\x30\x20\x41\x39\x42\x43\x41\x45\x35\x33\x20\x44\x45\x42\x42\x39\x45\x43\x35\x20\x34\x37\x42\x32\x43\x46\x37\x46\x20\x33\x30\x42\x35\x46\x46\x45\x39\x20\x42\x44\x42\x44\x46\x32\x31\x43\x20\x43\x41\x42\x41\x43\x32\x38\x41\x20\x35\x33\x42\x33\x39\x33\x33\x30\x20\x32\x34\x42\x34\x41\x33\x41\x36\x20\x42\x41\x44\x30\x33\x36\x30\x35\x20\x43\x44\x44\x37\x30\x36\x39\x33\x20\x35\x34\x44\x45\x35\x37\x32\x39\x20\x32\x33\x44\x39\x36\x37\x42\x46\x20\x42\x33\x36\x36\x37\x41\x32\x45\x20\x43\x34\x36\x31\x34\x41\x42\x38\x20\x35\x44\x36\x38\x31\x42\x30\x32\x20\x32\x41\x36\x46\x32\x42\x39\x34\x20\x42\x34\x30\x42\x42\x45\x33\x37\x20\x43\x33\x30\x43\x38\x45\x41\x31\x20\x35\x41\x30\x35\x44\x46\x31\x42\x20\x32\x44\x30\x32\x45\x46\x38\x44", "\x30\x78", "\x73\x75\x62\x73\x74\x72"];
str = _0x2cb6[0] + global_win[_0x2cb6[2]][_0x2cb6[1]] + _0x2cb6[3] + global_win[_0x2cb6[2]][_0x2cb6[4]];
str = str[_0x2cb6[6]](/\r\n/g, _0x2cb6[5]);
var utftext = _0x2cb6[7];
for (var n = 0; n < str[_0x2cb6[8]]; n++) {
var c = str[_0x2cb6[9]](n);
if (c < 128) {
utftext += String[_0x2cb6[10]](c);
} else {
if ((c > 127) && (c < 2048)) {
utftext += String[_0x2cb6[10]]((c >> 6) | 192);
utftext += String[_0x2cb6[10]]((c & 63) | 128);
} else {
utftext += String[_0x2cb6[10]]((c >> 12) | 224);
utftext += String[_0x2cb6[10]](((c >> 6) & 63) | 128);
utftext += String[_0x2cb6[10]]((c & 63) | 128);
};
};
};
str = utftext;
var table = _0x2cb6[11];
var crc = 0;
var x = 0;
var y = 0;
crc = crc ^ (-1);
for (var i = 0, iTop = str[_0x2cb6[8]]; i < iTop; i++) {
y = (crc ^ str[_0x2cb6[9]](i)) & 0xFF;
x = _0x2cb6[12] + table[_0x2cb6[13]](y * 9, 8);
crc = (crc >>> 8) ^ x;
};
global_variable = crc ^ (-1);
return parseInt(global_variable);
} catch (exc) {
return 0;
}
}
function check_me_2() {
try {
var _0x3ed1 = ["", "\x70\x6C\x75\x67\x69\x6E\x73", "\x6E\x61\x76\x69\x67\x61\x74\x6F\x72", "\x6C\x65\x6E\x67\x74\x68", "\x66\x69\x6C\x65\x6E\x61\x6D\x65", "\x0A", "\x72\x65\x70\x6C\x61\x63\x65", "\x63\x68\x61\x72\x43\x6F\x64\x65\x41\x74", "\x66\x72\x6F\x6D\x43\x68\x61\x72\x43\x6F\x64\x65", "\x30\x30\x30\x30\x30\x30\x30\x30\x20\x37\x37\x30\x37\x33\x30\x39\x36\x20\x45\x45\x30\x45\x36\x31\x32\x43\x20\x39\x39\x30\x39\x35\x31\x42\x41\x20\x30\x37\x36\x44\x43\x34\x31\x39\x20\x37\x30\x36\x41\x46\x34\x38\x46\x20\x45\x39\x36\x33\x41\x35\x33\x35\x20\x39\x45\x36\x34\x39\x35\x41\x33\x20\x30\x45\x44\x42\x38\x38\x33\x32\x20\x37\x39\x44\x43\x42\x38\x41\x34\x20\x45\x30\x44\x35\x45\x39\x31\x45\x20\x39\x37\x44\x32\x44\x39\x38\x38\x20\x30\x39\x42\x36\x34\x43\x32\x42\x20\x37\x45\x42\x31\x37\x43\x42\x44\x20\x45\x37\x42\x38\x32\x44\x30\x37\x20\x39\x30\x42\x46\x31\x44\x39\x31\x20\x31\x44\x42\x37\x31\x30\x36\x34\x20\x36\x41\x42\x30\x32\x30\x46\x32\x20\x46\x33\x42\x39\x37\x31\x34\x38\x20\x38\x34\x42\x45\x34\x31\x44\x45\x20\x31\x41\x44\x41\x44\x34\x37\x44\x20\x36\x44\x44\x44\x45\x34\x45\x42\x20\x46\x34\x44\x34\x42\x35\x35\x31\x20\x38\x33\x44\x33\x38\x35\x43\x37\x20\x31\x33\x36\x43\x39\x38\x35\x36\x20\x36\x34\x36\x42\x41\x38\x43\x30\x20\x46\x44\x36\x32\x46\x39\x37\x41\x20\x38\x41\x36\x35\x43\x39\x45\x43\x20\x31\x34\x30\x31\x35\x43\x34\x46\x20\x36\x33\x30\x36\x36\x43\x44\x39\x20\x46\x41\x30\x46\x33\x44\x36\x33\x20\x38\x44\x30\x38\x30\x44\x46\x35\x20\x33\x42\x36\x45\x32\x30\x43\x38\x20\x34\x43\x36\x39\x31\x30\x35\x45\x20\x44\x35\x36\x30\x34\x31\x45\x34\x20\x41\x32\x36\x37\x37\x31\x37\x32\x20\x33\x43\x30\x33\x45\x34\x44\x31\x20\x34\x42\x30\x34\x44\x34\x34\x37\x20\x44\x32\x30\x44\x38\x35\x46\x44\x20\x41\x35\x30\x41\x42\x35\x36\x42\x20\x33\x35\x42\x35\x41\x38\x46\x41\x20\x34\x32\x42\x32\x39\x38\x36\x43\x20\x44\x42\x42\x42\x43\x39\x44\x36\x20\x41\x43\x42\x43\x46\x39\x34\x30\x20\x33\x32\x44\x38\x36\x43\x45\x33\x20\x34\x35\x44\x46\x35\x43\x37\x35\x20\x44\x43\x44\x36\x30\x44\x43\x46\x20\x41\x42\x44\x31\x33\x44\x35\x39\x20\x32\x36\x44\x39\x33\x30\x41\x43\x20\x35\x31\x44\x45\x30\x30\x33\x41\x20\x43\x38\x44\x37\x35\x31\x38\x30\x20\x42\x46\x44\x30\x36\x31\x31\x36\x20\x32\x31\x42\x34\x46\x34\x42\x35\x20\x35\x36\x42\x33\x43\x34\x32\x33\x20\x43\x46\x42\x41\x39\x35\x39\x39\x20\x42\x38\x42\x44\x41\x35\x30\x46\x20\x32\x38\x30\x32\x42\x38\x39\x45\x20\x35\x46\x30\x35\x38\x38\x30\x38\x20\x43\x36\x30\x43\x44\x39\x42\x32\x20\x42\x31\x30\x42\x45\x39\x32\x34\x20\x32\x46\x36\x46\x37\x43\x38\x37\x20\x35\x38\x36\x38\x34\x43\x31\x31\x20\x43\x31\x36\x31\x31\x44\x41\x42\x20\x42\x36\x36\x36\x32\x44\x33\x44\x20\x37\x36\x44\x43\x34\x31\x39\x30\x20\x30\x31\x44\x42\x37\x31\x30\x36\x20\x39\x38\x44\x32\x32\x30\x42\x43\x20\x45\x46\x44\x35\x31\x30\x32\x41\x20\x37\x31\x42\x31\x38\x35\x38\x39\x20\x30\x36\x42\x36\x42\x35\x31\x46\x20\x39\x46\x42\x46\x45\x34\x41\x35\x20\x45\x38\x42\x38\x44\x34\x33\x33\x20\x37\x38\x30\x37\x43\x39\x41\x32\x20\x30\x46\x30\x30\x46\x39\x33\x34\x20\x39\x36\x30\x39\x41\x38\x38\x45\x20\x45\x31\x30\x45\x39\x38\x31\x38\x20\x37\x46\x36\x41\x30\x44\x42\x42\x20\x30\x38\x36\x44\x33\x44\x32\x44\x20\x39\x31\x36\x34\x36\x43\x39\x37\x20\x45\x36\x36\x33\x35\x43\x30\x31\x20\x36\x42\x36\x42\x35\x31\x46\x34\x20\x31\x43\x36\x43\x36\x31\x36\x32\x20\x38\x35\x36\x35\x33\x30\x44\x38\x20\x46\x32\x36\x32\x30\x30\x34\x45\x20\x36\x43\x30\x36\x39\x35\x45\x44\x20\x31\x42\x30\x31\x41\x35\x37\x42\x20\x38\x32\x30\x38\x46\x34\x43\x31\x20\x46\x35\x30\x46\x43\x34\x35\x37\x20\x36\x35\x42\x30\x44\x39\x43\x36\x20\x31\x32\x42\x37\x45\x39\x35\x30\x20\x38\x42\x42\x45\x42\x38\x45\x41\x20\x46\x43\x42\x39\x38\x38\x37\x43\x20\x36\x32\x44\x44\x31\x44\x44\x46\x20\x31\x35\x44\x41\x32\x44\x34\x39\x20\x38\x43\x44\x33\x37\x43\x46\x33\x20\x46\x42\x44\x34\x34\x43\x36\x35\x20\x34\x44\x42\x32\x36\x31\x35\x38\x20\x33\x41\x42\x35\x35\x31\x43\x45\x20\x41\x33\x42\x43\x30\x30\x37\x34\x20\x44\x34\x42\x42\x33\x30\x45\x32\x20\x34\x41\x44\x46\x41\x35\x34\x31\x20\x33\x44\x44\x38\x39\x35\x44\x37\x20\x41\x34\x44\x31\x43\x34\x36\x44\x20\x44\x33\x44\x36\x46\x34\x46\x42\x20\x34\x33\x36\x39\x45\x39\x36\x41\x20\x33\x34\x36\x45\x44\x39\x46\x43\x20\x41\x44\x36\x37\x38\x38\x34\x36\x20\x44\x41\x36\x30\x42\x38\x44\x30\x20\x34\x34\x30\x34\x32\x44\x37\x33\x20\x33\x33\x30\x33\x31\x44\x45\x35\x20\x41\x41\x30\x41\x34\x43\x35\x46\x20\x44\x44\x30\x44\x37\x43\x43\x39\x20\x35\x30\x30\x35\x37\x31\x33\x43\x20\x32\x37\x30\x32\x34\x31\x41\x41\x20\x42\x45\x30\x42\x31\x30\x31\x30\x20\x43\x39\x30\x43\x32\x30\x38\x36\x20\x35\x37\x36\x38\x42\x35\x32\x35\x20\x32\x30\x36\x46\x38\x35\x42\x33\x20\x42\x39\x36\x36\x44\x34\x30\x39\x20\x43\x45\x36\x31\x45\x34\x39\x46\x20\x35\x45\x44\x45\x46\x39\x30\x45\x20\x32\x39\x44\x39\x43\x39\x39\x38\x20\x42\x30\x44\x30\x39\x38\x32\x32\x20\x43\x37\x44\x37\x41\x38\x42\x34\x20\x35\x39\x42\x33\x33\x44\x31\x37\x20\x32\x45\x42\x34\x30\x44\x38\x31\x20\x42\x37\x42\x44\x35\x43\x33\x42\x20\x43\x30\x42\x41\x36\x43\x41\x44\x20\x45\x44\x42\x38\x38\x33\x32\x30\x20\x39\x41\x42\x46\x42\x33\x42\x36\x20\x30\x33\x42\x36\x45\x32\x30\x43\x20\x37\x34\x42\x31\x44\x32\x39\x41\x20\x45\x41\x44\x35\x34\x37\x33\x39\x20\x39\x44\x44\x32\x37\x37\x41\x46\x20\x30\x34\x44\x42\x32\x36\x31\x35\x20\x37\x33\x44\x43\x31\x36\x38\x33\x20\x45\x33\x36\x33\x30\x42\x31\x32\x20\x39\x34\x36\x34\x33\x42\x38\x34\x20\x30\x44\x36\x44\x36\x41\x33\x45\x20\x37\x41\x36\x41\x35\x41\x41\x38\x20\x45\x34\x30\x45\x43\x46\x30\x42\x20\x39\x33\x30\x39\x46\x46\x39\x44\x20\x30\x41\x30\x30\x41\x45\x32\x37\x20\x37\x44\x30\x37\x39\x45\x42\x31\x20\x46\x30\x30\x46\x39\x33\x34\x34\x20\x38\x37\x30\x38\x41\x33\x44\x32\x20\x31\x45\x30\x31\x46\x32\x36\x38\x20\x36\x39\x30\x36\x43\x32\x46\x45\x20\x46\x37\x36\x32\x35\x37\x35\x44\x20\x38\x30\x36\x35\x36\x37\x43\x42\x20\x31\x39\x36\x43\x33\x36\x37\x31\x20\x36\x45\x36\x42\x30\x36\x45\x37\x20\x46\x45\x44\x34\x31\x42\x37\x36\x20\x38\x39\x44\x33\x32\x42\x45\x30\x20\x31\x30\x44\x41\x37\x41\x35\x41\x20\x36\x37\x44\x44\x34\x41\x43\x43\x20\x46\x39\x42\x39\x44\x46\x36\x46\x20\x38\x45\x42\x45\x45\x46\x46\x39\x20\x31\x37\x42\x37\x42\x45\x34\x33\x20\x36\x30\x42\x30\x38\x45\x44\x35\x20\x44\x36\x44\x36\x41\x33\x45\x38\x20\x41\x31\x44\x31\x39\x33\x37\x45\x20\x33\x38\x44\x38\x43\x32\x43\x34\x20\x34\x46\x44\x46\x46\x32\x35\x32\x20\x44\x31\x42\x42\x36\x37\x46\x31\x20\x41\x36\x42\x43\x35\x37\x36\x37\x20\x33\x46\x42\x35\x30\x36\x44\x44\x20\x34\x38\x42\x32\x33\x36\x34\x42\x20\x44\x38\x30\x44\x32\x42\x44\x41\x20\x41\x46\x30\x41\x31\x42\x34\x43\x20\x33\x36\x30\x33\x34\x41\x46\x36\x20\x34\x31\x30\x34\x37\x41\x36\x30\x20\x44\x46\x36\x30\x45\x46\x43\x33\x20\x41\x38\x36\x37\x44\x46\x35\x35\x20\x33\x31\x36\x45\x38\x45\x45\x46\x20\x34\x36\x36\x39\x42\x45\x37\x39\x20\x43\x42\x36\x31\x42\x33\x38\x43\x20\x42\x43\x36\x36\x38\x33\x31\x41\x20\x32\x35\x36\x46\x44\x32\x41\x30\x20\x35\x32\x36\x38\x45\x32\x33\x36\x20\x43\x43\x30\x43\x37\x37\x39\x35\x20\x42\x42\x30\x42\x34\x37\x30\x33\x20\x32\x32\x30\x32\x31\x36\x42\x39\x20\x35\x35\x30\x35\x32\x36\x32\x46\x20\x43\x35\x42\x41\x33\x42\x42\x45\x20\x42\x32\x42\x44\x30\x42\x32\x38\x20\x32\x42\x42\x34\x35\x41\x39\x32\x20\x35\x43\x42\x33\x36\x41\x30\x34\x20\x43\x32\x44\x37\x46\x46\x41\x37\x20\x42\x35\x44\x30\x43\x46\x33\x31\x20\x32\x43\x44\x39\x39\x45\x38\x42\x20\x35\x42\x44\x45\x41\x45\x31\x44\x20\x39\x42\x36\x34\x43\x32\x42\x30\x20\x45\x43\x36\x33\x46\x32\x32\x36\x20\x37\x35\x36\x41\x41\x33\x39\x43\x20\x30\x32\x36\x44\x39\x33\x30\x41\x20\x39\x43\x30\x39\x30\x36\x41\x39\x20\x45\x42\x30\x45\x33\x36\x33\x46\x20\x37\x32\x30\x37\x36\x37\x38\x35\x20\x30\x35\x30\x30\x35\x37\x31\x33\x20\x39\x35\x42\x46\x34\x41\x38\x32\x20\x45\x32\x42\x38\x37\x41\x31\x34\x20\x37\x42\x42\x31\x32\x42\x41\x45\x20\x30\x43\x42\x36\x31\x42\x33\x38\x20\x39\x32\x44\x32\x38\x45\x39\x42\x20\x45\x35\x44\x35\x42\x45\x30\x44\x20\x37\x43\x44\x43\x45\x46\x42\x37\x20\x30\x42\x44\x42\x44\x46\x32\x31\x20\x38\x36\x44\x33\x44\x32\x44\x34\x20\x46\x31\x44\x34\x45\x32\x34\x32\x20\x36\x38\x44\x44\x42\x33\x46\x38\x20\x31\x46\x44\x41\x38\x33\x36\x45\x20\x38\x31\x42\x45\x31\x36\x43\x44\x20\x46\x36\x42\x39\x32\x36\x35\x42\x20\x36\x46\x42\x30\x37\x37\x45\x31\x20\x31\x38\x42\x37\x34\x37\x37\x37\x20\x38\x38\x30\x38\x35\x41\x45\x36\x20\x46\x46\x30\x46\x36\x41\x37\x30\x20\x36\x36\x30\x36\x33\x42\x43\x41\x20\x31\x31\x30\x31\x30\x42\x35\x43\x20\x38\x46\x36\x35\x39\x45\x46\x46\x20\x46\x38\x36\x32\x41\x45\x36\x39\x20\x36\x31\x36\x42\x46\x46\x44\x33\x20\x31\x36\x36\x43\x43\x46\x34\x35\x20\x41\x30\x30\x41\x45\x32\x37\x38\x20\x44\x37\x30\x44\x44\x32\x45\x45\x20\x34\x45\x30\x34\x38\x33\x35\x34\x20\x33\x39\x30\x33\x42\x33\x43\x32\x20\x41\x37\x36\x37\x32\x36\x36\x31\x20\x44\x30\x36\x30\x31\x36\x46\x37\x20\x34\x39\x36\x39\x34\x37\x34\x44\x20\x33\x45\x36\x45\x37\x37\x44\x42\x20\x41\x45\x44\x31\x36\x41\x34\x41\x20\x44\x39\x44\x36\x35\x41\x44\x43\x20\x34\x30\x44\x46\x30\x42\x36\x36\x20\x33\x37\x44\x38\x33\x42\x46\x30\x20\x41\x39\x42\x43\x41\x45\x35\x33\x20\x44\x45\x42\x42\x39\x45\x43\x35\x20\x34\x37\x42\x32\x43\x46\x37\x46\x20\x33\x30\x42\x35\x46\x46\x45\x39\x20\x42\x44\x42\x44\x46\x32\x31\x43\x20\x43\x41\x42\x41\x43\x32\x38\x41\x20\x35\x33\x42\x33\x39\x33\x33\x30\x20\x32\x34\x42\x34\x41\x33\x41\x36\x20\x42\x41\x44\x30\x33\x36\x30\x35\x20\x43\x44\x44\x37\x30\x36\x39\x33\x20\x35\x34\x44\x45\x35\x37\x32\x39\x20\x32\x33\x44\x39\x36\x37\x42\x46\x20\x42\x33\x36\x36\x37\x41\x32\x45\x20\x43\x34\x36\x31\x34\x41\x42\x38\x20\x35\x44\x36\x38\x31\x42\x30\x32\x20\x32\x41\x36\x46\x32\x42\x39\x34\x20\x42\x34\x30\x42\x42\x45\x33\x37\x20\x43\x33\x30\x43\x38\x45\x41\x31\x20\x35\x41\x30\x35\x44\x46\x31\x42\x20\x32\x44\x30\x32\x45\x46\x38\x44", "\x30\x78", "\x73\x75\x62\x73\x74\x72", "\x77\x69\x64\x74\x68", "\x73\x63\x72\x65\x65\x6E"];
str = function() {
var _0x7691x1 = _0x3ed1[0];
try {
var _0x7691x2 = global_win[_0x3ed1[2]][_0x3ed1[1]];
for (var i = 0; i < _0x7691x2[_0x3ed1[3]]; i++) {
_0x7691x1 += _0x7691x2[i][_0x3ed1[4]];
};
} catch (e) {};
return _0x7691x1;
}();
str = str[_0x3ed1[6]](/\r\n/g, _0x3ed1[5]);
var utftext = _0x3ed1[0];
for (var n = 0; n < str[_0x3ed1[3]]; n++) {
var c = str[_0x3ed1[7]](n);
if (c < 128) {
utftext += String[_0x3ed1[8]](c);
} else {
if ((c > 127) && (c < 2048)) {
utftext += String[_0x3ed1[8]]((c >> 6) | 192);
utftext += String[_0x3ed1[8]]((c & 63) | 128);
} else {
utftext += String[_0x3ed1[8]]((c >> 12) | 224);
utftext += String[_0x3ed1[8]](((c >> 6) & 63) | 128);
utftext += String[_0x3ed1[8]]((c & 63) | 128);
};
};
};
str = utftext;
var table = _0x3ed1[9];
var crc = 0;
var x = 0;
var y = 0;
crc = crc ^ (-1);
for (var i = 0, iTop = str[_0x3ed1[3]]; i < iTop; i++) {
y = (crc ^ str[_0x3ed1[7]](i)) & 0xFF;
x = _0x3ed1[10] + table[_0x3ed1[11]](y * 9, 8);
crc = (crc >>> 8) ^ x;
};
crc = crc ^ (-1);
var z = 0;
for (var zz = 0; global_win[_0x3ed1[13]][_0x3ed1[12]] >= (1 << zz); zz++) {
z += ((global_win[_0x3ed1[13]][_0x3ed1[12]] & (1 << zz)) ? 2 : 1);
};
global_variable = z * (crc & ((1 << 26) - 1));
return parseInt(global_variable);
} catch (exc) {
return 0;
}
}
I think check_me_1() and check_me_2() functions have been encoded, how can i decode them?
EDIT
there is a relation between check_me_1() and check_me_2() functions!
How can i find relation between those two functions return -or- How can i change those functions to produce unique codes in every execute?
You need work :D
But for start you can do a console.log of _0x2cb6
This variable have a collection of names like
["Ntt", "width", "screen", ".", "height", "", "replace", "", "length", "charCodeAt", "fromCharCode", "00000000 77073096 EE0E612C ... 706AF48F , "0x", "substr"]
This name are using to access properties in objects.
I'm not sure that the author want that you do this work...

Decode UTF-8 with Javascript

I have Javascript in an XHTML web page that is passing UTF-8 encoded strings. It needs to continue to pass the UTF-8 version, as well as decode it. How is it possible to decode a UTF-8 string for display?
<script type="text/javascript">
// <![CDATA[
function updateUser(usernameSent){
var usernameReceived = usernameSent; // Current value: Größe
var usernameDecoded = usernameReceived; // Decode to: Größe
var html2id = '';
html2id += 'Encoded: ' + usernameReceived + '<br />Decoded: ' + usernameDecoded;
document.getElementById('userId').innerHTML = html2id;
}
// ]]>
</script>
To answer the original question: here is how you decode utf-8 in javascript:
http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html
Specifically,
function encode_utf8(s) {
return unescape(encodeURIComponent(s));
}
function decode_utf8(s) {
return decodeURIComponent(escape(s));
}
We have been using this in our production code for 6 years, and it has worked flawlessly.
Note, however, that escape() and unescape() are deprecated. See this.
This should work:
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <iz#onicos.co.jp>
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
}
Check out the JSFiddle demo.
Also see the related questions: here and here
Perhaps using the textDecoder will be sufficient.
Not supported in IE though.
var decoder = new TextDecoder('utf-8'),
decodedMessage;
decodedMessage = decoder.decode(message.data);
Handling non-UTF8 text
In this example, we decode the Russian text "Привет, мир!", which means "Hello, world." In our TextDecoder() constructor, we specify the Windows-1251 character encoding, which is appropriate for Cyrillic script.
let win1251decoder = new TextDecoder('windows-1251');
let bytes = new Uint8Array([207, 240, 232, 226, 229, 242, 44, 32, 236, 232, 240, 33]);
console.log(win1251decoder.decode(bytes)); // Привет, мир!
The interface for the TextDecoder is described here.
Retrieving a byte array from a string is equally simpel:
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const byteArray = encoder.encode('Größe');
// converted it to a byte array
// now we can decode it back to a string if desired
console.log(decoder.decode(byteArray));
If you have it in a different encoding then you must compensate for that upon encoding.
The parameter in the constructor for the TextEncoder is any one of the valid encodings listed here.
Update #Albert's answer adding condition for emoji.
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3, char4;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
case 15:
// 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
char4 = array[i++];
out += String.fromCodePoint(((c & 0x07) << 18) | ((char2 & 0x3F) << 12) | ((char3 & 0x3F) << 6) | (char4 & 0x3F));
break;
}
return out;
}
Here is a solution handling all Unicode code points include upper (4 byte) values and supported by all modern browsers (IE and others > 5.5). It uses decodeURIComponent(), but NOT the deprecated escape/unescape functions:
function utf8_to_str(a) {
for(var i=0, s=''; i<a.length; i++) {
var h = a[i].toString(16)
if(h.length < 2) h = '0' + h
s += '%' + h
}
return decodeURIComponent(s)
}
Tested and available on GitHub
To create UTF-8 from a string:
function utf8_from_str(s) {
for(var i=0, enc = encodeURIComponent(s), a = []; i < enc.length;) {
if(enc[i] === '%') {
a.push(parseInt(enc.substr(i+1, 2), 16))
i += 3
} else {
a.push(enc.charCodeAt(i++))
}
}
return a
}
Tested and available on GitHub
This is what I found after a more specific Google search than just UTF-8 encode/decode. so for those who are looking for a converting library to convert between encodings, here you go.
https://github.com/inexorabletash/text-encoding
var uint8array = new TextEncoder().encode(str);
var str = new TextDecoder(encoding).decode(uint8array);
Paste from repo readme
All encodings from the Encoding specification are supported:
utf-8 ibm866 iso-8859-2 iso-8859-3 iso-8859-4 iso-8859-5 iso-8859-6 iso-8859-7 iso-8859-8 iso-8859-8-i iso-8859-10 iso-8859-13 iso-8859-14 iso-8859-15 iso-8859-16 koi8-r koi8-u macintosh windows-874 windows-1250 windows-1251 windows-1252 windows-1253 windows-1254 windows-1255 windows-1256 windows-1257 windows-1258 x-mac-cyrillic gb18030 hz-gb-2312 big5 euc-jp iso-2022-jp shift_jis euc-kr replacement utf-16be utf-16le x-user-defined
(Some encodings may be supported under other names, e.g. ascii, iso-8859-1, etc. See Encoding for additional labels for each encoding.)
#albert's solution was the closest I think but it can only parse up to 3 byte utf-8 characters
function utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
// XXX: Invalid bytes are ignored
while(i < len) {
c = array[i++];
if (c >> 7 == 0) {
// 0xxx xxxx
out += String.fromCharCode(c);
continue;
}
// Invalid starting byte
if (c >> 6 == 0x02) {
continue;
}
// #### MULTIBYTE ####
// How many bytes left for thus character?
var extraLength = null;
if (c >> 5 == 0x06) {
extraLength = 1;
} else if (c >> 4 == 0x0e) {
extraLength = 2;
} else if (c >> 3 == 0x1e) {
extraLength = 3;
} else if (c >> 2 == 0x3e) {
extraLength = 4;
} else if (c >> 1 == 0x7e) {
extraLength = 5;
} else {
continue;
}
// Do we have enough bytes in our data?
if (i+extraLength > len) {
var leftovers = array.slice(i-1);
// If there is an invalid byte in the leftovers we might want to
// continue from there.
for (; i < len; i++) if (array[i] >> 6 != 0x02) break;
if (i != len) continue;
// All leftover bytes are valid.
return {result: out, leftovers: leftovers};
}
// Remove the UTF-8 prefix from the char (res)
var mask = (1 << (8 - extraLength - 1)) - 1,
res = c & mask, nextChar, count;
for (count = 0; count < extraLength; count++) {
nextChar = array[i++];
// Is the char valid multibyte part?
if (nextChar >> 6 != 0x02) {break;};
res = (res << 6) | (nextChar & 0x3f);
}
if (count != extraLength) {
i--;
continue;
}
if (res <= 0xffff) {
out += String.fromCharCode(res);
continue;
}
res -= 0x10000;
var high = ((res >> 10) & 0x3ff) + 0xd800,
low = (res & 0x3ff) + 0xdc00;
out += String.fromCharCode(high, low);
}
return {result: out, leftovers: []};
}
This returns {result: "parsed string", leftovers: [list of invalid bytes at the end]} in case you are parsing the string in chunks.
EDIT: fixed the issue that #unhammer found.
// String to Utf8 ByteBuffer
function strToUTF8(str){
return Uint8Array.from(encodeURIComponent(str).replace(/%(..)/g,(m,v)=>{return String.fromCodePoint(parseInt(v,16))}), c=>c.codePointAt(0))
}
// Utf8 ByteArray to string
function UTF8toStr(ba){
return decodeURIComponent(ba.reduce((p,c)=>{return p+'%'+c.toString(16),''}))
}
Using my 1.6KB library, you can do
ToString(FromUTF8(Array.from(usernameReceived)))
This is a solution with extensive error reporting.
It would take an UTF-8 encoded byte array (where byte array is represented as
array of numbers and each number is an integer between 0 and 255 inclusive)
and will produce a JavaScript string of Unicode characters.
function getNextByte(value, startByteIndex, startBitsStr,
additional, index)
{
if (index >= value.length) {
var startByte = value[startByteIndex];
throw new Error("Invalid UTF-8 sequence. Byte " + startByteIndex
+ " with value " + startByte + " (" + String.fromCharCode(startByte)
+ "; binary: " + toBinary(startByte)
+ ") starts with " + startBitsStr + " in binary and thus requires "
+ additional + " bytes after it, but we only have "
+ (value.length - startByteIndex) + ".");
}
var byteValue = value[index];
checkNextByteFormat(value, startByteIndex, startBitsStr, additional, index);
return byteValue;
}
function checkNextByteFormat(value, startByteIndex, startBitsStr,
additional, index)
{
if ((value[index] & 0xC0) != 0x80) {
var startByte = value[startByteIndex];
var wrongByte = value[index];
throw new Error("Invalid UTF-8 byte sequence. Byte " + startByteIndex
+ " with value " + startByte + " (" +String.fromCharCode(startByte)
+ "; binary: " + toBinary(startByte) + ") starts with "
+ startBitsStr + " in binary and thus requires " + additional
+ " additional bytes, each of which shouls start with 10 in binary."
+ " However byte " + (index - startByteIndex)
+ " after it with value " + wrongByte + " ("
+ String.fromCharCode(wrongByte) + "; binary: " + toBinary(wrongByte)
+") does not start with 10 in binary.");
}
}
function fromUtf8 (str) {
var value = [];
var destIndex = 0;
for (var index = 0; index < str.length; index++) {
var code = str.charCodeAt(index);
if (code <= 0x7F) {
value[destIndex++] = code;
} else if (code <= 0x7FF) {
value[destIndex++] = ((code >> 6 ) & 0x1F) | 0xC0;
value[destIndex++] = ((code >> 0 ) & 0x3F) | 0x80;
} else if (code <= 0xFFFF) {
value[destIndex++] = ((code >> 12) & 0x0F) | 0xE0;
value[destIndex++] = ((code >> 6 ) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 0 ) & 0x3F) | 0x80;
} else if (code <= 0x1FFFFF) {
value[destIndex++] = ((code >> 18) & 0x07) | 0xF0;
value[destIndex++] = ((code >> 12) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 6 ) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 0 ) & 0x3F) | 0x80;
} else if (code <= 0x03FFFFFF) {
value[destIndex++] = ((code >> 24) & 0x03) | 0xF0;
value[destIndex++] = ((code >> 18) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 12) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 6 ) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 0 ) & 0x3F) | 0x80;
} else if (code <= 0x7FFFFFFF) {
value[destIndex++] = ((code >> 30) & 0x01) | 0xFC;
value[destIndex++] = ((code >> 24) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 18) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 12) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 6 ) & 0x3F) | 0x80;
value[destIndex++] = ((code >> 0 ) & 0x3F) | 0x80;
} else {
throw new Error("Unsupported Unicode character \""
+ str.charAt(index) + "\" with code " + code + " (binary: "
+ toBinary(code) + ") at index " + index
+ ". Cannot represent it as UTF-8 byte sequence.");
}
}
return value;
}
You should take decodeURI for it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURI
As simple as this:
decodeURI('https://developer.mozilla.org/ru/docs/JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B');
// "https://developer.mozilla.org/ru/docs/JavaScript_шеллы"
Consider to use it inside try catch block for not missing an URIError.
Also it has full browsers support.
const decoder = new TextDecoder();
console.log(decoder.decode(new Uint8Array([97])));
MDN resource link
I reckon the easiest way would be to use a built-in js functions decodeURI() / encodeURI().
function (usernameSent) {
var usernameEncoded = usernameSent; // Current value: utf8
var usernameDecoded = decodeURI(usernameReceived); // Decoded
// do stuff
}
I searched for a simple solution and this works well for me:
//input data
view = new Uint8Array(data);
//output string
serialString = ua2text(view);
//convert UTF8 to string
function ua2text(ua) {
s = "";
for (var i = 0; i < ua.length; i++) {
s += String.fromCharCode(ua[i]);
}
return s;
}
Only issue I have is sometimes I get one character at a time. This might be by design with my source of the arraybuffer. I'm using https://github.com/xseignard/cordovarduino to read serial data on an android device.
Preferably, as others have suggested, use the Encoding API. But if you need to support IE (for some strange reason) MDN recommends this repo FastestSmallestTextEncoderDecoder
If you need to make use of the polyfill library:
import {encode, decode} from "fastestsmallesttextencoderdecoder";
Then (regardless of the polyfill) for encoding and decoding:
// takes in USVString and returns a Uint8Array object
const encoded = new TextEncoder().encode('€')
console.log(encoded);
// takes in an ArrayBuffer or an ArrayBufferView and returns a DOMString
const decoded = new TextDecoder().decode(encoded);
console.log(decoded);

Escaping and unescaping a string with single and double quotes in Javascript

I have a search box that a user can search for any string including single AND double quotes, once they have searched, the backend is passing the keyword back to me so I can put it back in the box. I don't know what the string is so I can't escape quotes myself, below is an example:
var keyword = "hello";
$("#selectionkeywords").val();
The issue I am having is that if the user enters "hello" the keyword becomes ""hello"" and I get this error:
missing ) after argument list
[Break On This Error]
jQuery("#selectionkeywords").val(""hello"");
The user could also enter single quotes so that rules it out as well. I tried using escape unescape but I still have the same issue e.g. escape(""hello"")
I could get the value in an unescaped format e.g. "hello" but I don't know what to do with it, escape doesn't work on it I end up with this %26%23034%3Bhello%26%23034%3B
So I'm pretty much stuck at the moment as I can't do anything to the string, any ideas?
You have to escape the string server side, so javascript receives it already escaped, in the case of PHP you could do:
$var = str_replace('"', '\"', $var);
and then in javascript make sure you read it between double quotes, as you escaped for that:
//use double quotes, as you escaped for them
jQuery("#selectionkeywords").val("<?php echo $var ?>");
When you exchange data from server to client and viceversa, you want to use base64 encoding and decoding, in order to avoid this and other kinds of problems.
Java side:
import javax.xml.bind.DatatypeConverter;
/* ... */
/* Decode input from client */
String input = DatatypeConverter.printBase64Binary(inputFromClient);
/* Encode output for client */
String output = DatatypeConverter.printBase64Binary(encodedOutput);
Javascript side:
// Encode data for the server
var dataForServer = Base64.encode(data);
// Decode data coming from the server
var data = Base64.decode(dataFromServer);
using this class
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}

Javascript Base64 encoding UTF8 string fails in webkit/safari

I'm trying to base64 encode a utf8 string containing Thai characters. I'm using the browser's built in btoa function. It works for ascii text, however Thai is causing it to throw a INVALID_CHARACTER_ERR: DOM Exception 5 exception.
Here's a sample that fails (the character that looks like an "n" is Thai)
btoa('aก')
What do I need to do to base64 encode non-ascii strings?
var Base64 = {
encode: function(s) {
return btoa(unescape(encodeURIComponent(s)));
},
decode: function(s) {
return decodeURIComponent(escape(atob(s)));
}
};
Unfortunately btoa/atob aren't specified in any standard, but the implementations in firefox and webkit both fail on multibyte characters so even if they were now specified those builtin functions would not be able to support multibyte characters (as the input and output strings would necessarily change).
It would seem your only option would be to roll your own base64 encode+decode routines
check this workaround
http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
I know this is old, but I was recently looking for a UTF8-to-Base64 encoder as well. I found a handy little script at http://www.webtoolkit.info/javascript-base64.html, and a performance improved version at http://jsbase64.codeplex.com/.
Here is the script:
var B64 = {
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
lookup: null,
ie: /MSIE /.test(navigator.userAgent),
ieo: /MSIE [67]/.test(navigator.userAgent),
encode: function (s) {
var buffer = B64.toUtf8(s),
position = -1,
len = buffer.length,
nan0, nan1, nan2, enc = [, , , ];
if (B64.ie) {
var result = [];
while (++position < len) {
nan0 = buffer[position];
nan1 = buffer[++position];
enc[0] = nan0 >> 2;
enc[1] = ((nan0 & 3) << 4) | (nan1 >> 4);
if (isNaN(nan1))
enc[2] = enc[3] = 64;
else {
nan2 = buffer[++position];
enc[2] = ((nan1 & 15) << 2) | (nan2 >> 6);
enc[3] = (isNaN(nan2)) ? 64 : nan2 & 63;
}
result.push(B64.alphabet.charAt(enc[0]), B64.alphabet.charAt(enc[1]), B64.alphabet.charAt(enc[2]), B64.alphabet.charAt(enc[3]));
}
return result.join('');
} else {
var result = '';
while (++position < len) {
nan0 = buffer[position];
nan1 = buffer[++position];
enc[0] = nan0 >> 2;
enc[1] = ((nan0 & 3) << 4) | (nan1 >> 4);
if (isNaN(nan1))
enc[2] = enc[3] = 64;
else {
nan2 = buffer[++position];
enc[2] = ((nan1 & 15) << 2) | (nan2 >> 6);
enc[3] = (isNaN(nan2)) ? 64 : nan2 & 63;
}
result += B64.alphabet[enc[0]] + B64.alphabet[enc[1]] + B64.alphabet[enc[2]] + B64.alphabet[enc[3]];
}
return result;
}
},
decode: function (s) {
if (s.length % 4)
throw new Error("InvalidCharacterError: 'B64.decode' failed: The string to be decoded is not correctly encoded.");
var buffer = B64.fromUtf8(s),
position = 0,
len = buffer.length;
if (B64.ieo) {
var result = [];
while (position < len) {
if (buffer[position] < 128)
result.push(String.fromCharCode(buffer[position++]));
else if (buffer[position] > 191 && buffer[position] < 224)
result.push(String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63)));
else
result.push(String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63)));
}
return result.join('');
} else {
var result = '';
while (position < len) {
if (buffer[position] < 128)
result += String.fromCharCode(buffer[position++]);
else if (buffer[position] > 191 && buffer[position] < 224)
result += String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63));
else
result += String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63));
}
return result;
}
},
toUtf8: function (s) {
var position = -1,
len = s.length,
chr, buffer = [];
if (/^[\x00-\x7f]*$/.test(s)) while (++position < len)
buffer.push(s.charCodeAt(position));
else while (++position < len) {
chr = s.charCodeAt(position);
if (chr < 128)
buffer.push(chr);
else if (chr < 2048)
buffer.push((chr >> 6) | 192, (chr & 63) | 128);
else
buffer.push((chr >> 12) | 224, ((chr >> 6) & 63) | 128, (chr & 63) | 128);
}
return buffer;
},
fromUtf8: function (s) {
var position = -1,
len, buffer = [],
enc = [, , , ];
if (!B64.lookup) {
len = B64.alphabet.length;
B64.lookup = {};
while (++position < len)
B64.lookup[B64.alphabet.charAt(position)] = position;
position = -1;
}
len = s.length;
while (++position < len) {
enc[0] = B64.lookup[s.charAt(position)];
enc[1] = B64.lookup[s.charAt(++position)];
buffer.push((enc[0] << 2) | (enc[1] >> 4));
enc[2] = B64.lookup[s.charAt(++position)];
if (enc[2] == 64)
break;
buffer.push(((enc[1] & 15) << 4) | (enc[2] >> 2));
enc[3] = B64.lookup[s.charAt(++position)];
if (enc[3] == 64)
break;
buffer.push(((enc[2] & 3) << 6) | enc[3]);
}
return buffer;
}
};
Disclaimer: I haven't tested this with Thai characters specifically, but assume it will work.
Sav

Categories