Skip to content

Instantly share code, notes, and snippets.

@fermuch
Created January 17, 2012 13:58
Show Gist options
  • Save fermuch/1626702 to your computer and use it in GitHub Desktop.
Save fermuch/1626702 to your computer and use it in GitHub Desktop.
Library for Hexadecimal working with Javascript. I based this on a lib that I found in a blog, but I don't remember the blog, so I'm sorry.
var HexConverter = {
hexDigits : '0123456789ABCDEF',
dec2hex : function( dec )
{
return( this.hexDigits[ dec >> 4 ] + this.hexDigits[ dec & 15 ] );
},
hex2dec : function( hex )
{
return( parseInt( hex, 16 ) )
},
hex2str : function ( hex )
{
return ( String.fromCharCode( this.hex2dec(hex) ) )
},
arr2str : function ( arr, base64 )
{
var str = "";
for (a in arr){
str += this.hex2str(arr[a]);
}
if(base64 == true){
str = Base64.encode(str);
}
return (str);
},
str2hex : function ( str )
{
var arr = [];
for (a in str){
var charcode = str[a].charCodeAt();
arr.push(this.dec2hex(charcode));
}
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment