<?php
/*
 * Author: ynori7
 * Copyright: halls-of-valhalla.org
 */

/*****************************************************
 * Converts a character to its corresponding int:
 * chToInt('0') => 0, chToInt('a') => 11, chToInt('B') => 12, etc.
 ******************************************************/
function chToInt($c) {
  $c = strtolower($c);
  $ls="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $pos = strpos($ls, $c);
  if($pos !== false)
    return $pos;
  return 0;//If somehow we got some other random character just assume it's 0
}

/*************************************************
 * Converts an int to its corresponding char:
 * intToCh(1) => '1', intToCh(11) => 'a', intToCh(15) => 'f'
 *******************************************************/
function intToCh($i) {
  $ls="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  return $ls[$i];
}

/***********************************************
 * validate: validates the input to ensure it's actually
 *           in the base indicated.
 **********************************************/
function validate($mInput, $mBase) {
  for($x=0; $x<strlen($mInput); $x++)
  {
    if(chToInt($mInput[$x])>$mBase-1)
    {
      return false;
    }
  }
  return true;
}

/***********************************************
 * convert: converts the string mInput from base
 *              mBase1 to mBase2, and returns a string representation
 ***********************************************/
function convert($mInput, $mBase1, $mBase2) {
  if(!validate($mInput, $mBase1)){
    return "Invalid.\n";
  }
  $bTen=chToInt($mInput[0]);
  $bTenDec=0;
  $point=0;
  $fraction="";
  $whole="";
  $x=0; $y=0; $w=0; $p=0; $exp=0;

  /******GET NUMBER******/
  for($x=1; $x<strlen($mInput); $x++)//convert the whole part of the number into base 10
  {
    if($mInput[$x]=='.'){$point=1; break;}//we got to a fractional portion
    $bTen*=$mBase1;
    $bTen+=chToInt($mInput[$x]);
  }

  if($point==1)//convert the fraction part of the number into base 10
  {
    $exp=1;
    for($y=$x+1; $y<strlen($mInput); $y++)
    {
      $bTenDec+=chToInt($mInput[$y])* pow($mBase1, -$exp);
      $exp++;
    }
  }
///////////////////////////////////////////////////////////////
  /******COVERT WHOLE PORTION TO CORRECT BASE*****/
  while( $bTen > 0 )//convert the whole part of the base 10 number to the new base
  {
    $whole .= intToCh($bTen%$mBase2);
    $bTen = ($bTen-$bTen%$mBase2)/$mBase2;
  }

  $whole = strrev($whole); //reverse it
///////////////////////////////////////////////////////////////
  /*****CONVERT FRACTIONAL PORTION*******/
  while(strlen($fraction)<5)//convert the fraction part of the base 10 number to the new base, precision 5
  {
    $bTenDec*=$mBase2;
    if(floor($bTenDec)<$bTenDec)
    {
      $fraction .= intToCh(floor($bTenDec));
      $bTenDec-=floor($bTenDec);
    }
    else if(floor($bTenDec)==$bTenDec)
    {
      $fraction .= intToCh((int)$bTenDec);
      break;
    }
    $y++;
  }
  while(strlen($fraction)<5)
  {
      $fraction .= '0';
  }

  return "$whole.$fraction"; //combine and return result
}

if(isset($_POST['num']))
{
  $num = $_POST['num'];
  $base1 = (int)$_POST['base1'];
  $base2 = (int)$_POST['base2'];
  echo "<p>Result: ".convert($num, $base1, $base2) . "</p>\n";
}

echo "<form action='' method='POST'>
<table>
<tr>
  <td>Number To Convert:</td>
  <td><input type='text' name='num' maxlength='50'></td>
</tr>
<tr>
  <td>Original Base:</td>
  <td><input type='text' name='base1' maxlength='2'></td>
</tr>
<tr>
  <td>Base To Convert To:</td>
  <td><input type='text' name='base2' maxlength='2'></td>
</tr>
<tr>
  <td colspan='2'><input type='submit' value='submit'></td>
</tr>
</table>
</form>\n";
?>