Created
June 26, 2014 08:29
-
-
Save m1el/11ac1cc4bb0b40f201d7 to your computer and use it in GitHub Desktop.
functions for binary strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* little endian parser */ | |
function lit2int(str){ | |
var ret=0; | |
for(var i=str.length-1;i>=0;i--){ | |
ret=ret*256+(str.charCodeAt(i)&0xff); | |
} | |
return ret; | |
} | |
/* big endian parser */ | |
function bin2int(str){ | |
var ret=0; | |
for(var i=0;i<str.length;i++){ | |
ret=ret*256 + (str.charCodeAt(i)&0xff); | |
} | |
return ret; | |
} | |
function js_enconv(e){ | |
return bin2utf8(e)||cp12512utf8(e); | |
} | |
/** | |
* | |
* UTF-8 data decode | |
* http://www.webtoolkit.info/ | |
* | |
**/ | |
function bin2utf8(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); | |
if(c2>0xDF){//this is exeption! | |
console.log("illegal character sequence at "+i); | |
return null; | |
} | |
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); | |
i += 2; | |
}else { | |
c2 = utftext.charCodeAt(i+1); | |
c3 = utftext.charCodeAt(i+2); | |
if(c2>0xBF||c3>0xBF){ | |
console.log("illegal character sequence at "+i); | |
return null; | |
} | |
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); | |
i += 3; | |
} | |
} | |
return string; | |
} | |
function cp12512utf8 (cp12) { | |
var alphabet="ЂЃ‚ѓ„…†‡€‰Љ‹ЊЌЋЏђ‘’“”•–—™љ›њќћџ ЎўЈ¤Ґ¦§Ё©Є«¬®Ї°±Ііґµ¶·ё№є»јЅѕї" | |
+"АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя" | |
var ret=""; | |
var c; | |
for(var i=0;i<cp12.length;i++){ | |
c=cp12.charCodeAt(i)&255; | |
if(c<0x80){ | |
ret+=String.fromCharCode(c); | |
}else{ | |
ret+=alphabet.charAt(c-0x80); | |
} | |
} | |
return ret; | |
} | |
function cp8662utf(cp8){ | |
var alphabet="АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмноп░▒▓│┤╡╢╖╕╣║╗╝╜╛┐" | |
+"└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀рстуфхцчшщъыьэюяЁёЄєЇїЎў°∙·√№¤■ "; | |
var ret=""; | |
var c; | |
for(var i=0;i<cp8.length;i++){ | |
c=cp8.charCodeAt(i)&255; | |
if(c<0x80){ | |
ret+=String.fromCharCode(c); | |
}else{ | |
ret+=alphabet.charAt(c-0x80); | |
} | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment