Decimal to Hex Function

Home

This function converts a decimal number into a hex string.

//**************************************
//     
// Name: Javascript Decimal to Hex
// Description:This code allows you to e
//     asily convert a number into a hex string
//     
// By: Ben White
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/xq/ASP/txtCod
//     eId.2997/lngWId.2/qx/vb/scripts/ShowCode
//     .htm//for details.//**************************************
//
 
function Hex(int) {
  r = '';
  H = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  for(x=0;x<32;x++) {
    if (Math.pow(16,x+1) > int) { break }
  }
  for (y=x;y>=0;y--) {
    z = Math.pow(16,y);
    r+=H[Math.floor(int/z)];
    int = int % z;
  }
  return (r.length == 0)?'0':r;
}