Say you have a byte string: "0100010101110001010..."
. How to convert it into a UTF16 string (eg: "A|b☮"), and how to convert it back to the original byte string?
I have attempted the implementation below, but it seems like my understanding in UTF16 is not good enough and the code breaks in some (I don't know which) cases.
var pad = function(x){
while(x.length%16!==0)
x="0"+x;
return x;
}
var unpack_bin = function(a){
for(var r="",i=0,l=a.length;i<l;++i)
r+=pad((a[i].charCodeAt(0)-36).toString(2));
return r.slice(r.indexOf("1")+1);
}
var pack_bin = function(a) {
for (var s="",i=0,l=a.length,a=pad("1"+a);i<l;i+=16)
s+=String.fromCharCode(parseInt(a.slice(i,i+16),2)+36);
return s;
}