Base 10 to base N

by Jon Dinham

JavaScript

var digits = [
  '0','1','2','3','4','5','6','7','8','9',
  'A','B','C','D','E','F','G','H','I','J',
  'K','L','M','N','O','P','Q','R','S','T',
  'U','V','W','X','Y','Z'
];

/**
 * x is the base, y is the decimal value
 * x must be in the range of 2..36
 * y must be greater than zero
 */
function base10ToBaseN(x,y) {
  var str = "";
  
  while (y>0) {
    var mod = y%x;
    var div = Math.floor(y/x);
     
    str = digits[mod]+str;
    y   = div;
  }
    
  return str;
}

alert(base10ToBaseN(2,23));