Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Forked from taterbase/bin2string.js
Last active July 3, 2016 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myfreeer/055d00ea02395482aa1405bd82d246e7 to your computer and use it in GitHub Desktop.
Save myfreeer/055d00ea02395482aa1405bd82d246e7 to your computer and use it in GitHub Desktop.
Convert bytes to string Javascript
function str2arr(str){
var array_=new Array();
for(var i = 0; i < str.length; ++i){
array_[i]=str.charCodeAt(i);};
return array_;
}
function hex2arr(str){
var b_=new Uint8Array();
eval("b_=["+str+"]");
return b_;
}
function bin2string(array){
var result = "";
for(var i = 0; i < array.length; ++i){
result+= (String.fromCharCode(array[i]));
}
return result;
}
function hex2string(str){
return eval('String.fromCharCode('+str+')');
}
var hexstr="0x23,0x66,0x78,0x85,0xff,0x00"
bin2string(hex2arr(hexstr));
hex2string(hexstr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment