Class Base-36 (alphabets and numbers as digits) conversion

Recently I find me writing code that requires having a “code” for each item saved. To make my tasks easier and portable, I have created a PHP class called base36 to automate the task of converting to and from base10 (integer). The class is included below. A second class base26 is included in case you’d prefer to use just alphabets.

How do I mean?

Say you create a shopping cart and need to have order codes for each order on the site, using numbers can be very clumsy. Or you have a database of customers. Your customers will mostly get a numeric code assigned to them. Very few can remember the code easily.

Now for the scenarios depicted above, imagine that you could assign an alphanumeric non-case-sensitive code. It takes away the stress of having to remember very long numbers. A customer with order code “AGOOD1” is less likely to forget or even get it wrong than if the order code were say “632686933” (That’s the equivalent in base36).

However, note that with the class included below, the biggest code you can get is “ZIK0ZJ”. This is due to the integer limit of PHP being 2147483647. I had to copy and paste that even after knowng it for over 8 years! ZIK0ZJ, on the other hand I just knew yesterday and could type by hand. #justsaying. And the highest for base26 is “GYTISYX”.

Here are the classes:

Base 36:

class base36{
  public static function from10($base10){
   // $base10 stores a value in base 10
   // solution will have digits in an array

   if (!is_numeric($base10)){
      return '0';
      //if this were python I'da thrown an exception
      //but this is PHP #SMH
   }
   if ($base10>2147483647){
      return '0';
      //same excuse as above
   }

   $stringrep='';
   $index = 0 ;
   while ( $base10 > 0 )
    {
       $remainder = $base10 % 36 ;
       $base10       = (int) ($base10 / 36) ;  // integer division
       $stringrep =  base36::mychr($remainder) . $stringrep;
       $index ++ ;
    }

    if ( $stringrep==''){
      return '0';
    }

    return $stringrep;
  }

  public static function to10($base36){
    //only upper case, unless we'd like to do a base 62!
    $base36 = strtoupper($base36);

    //i'da loved to do thorough check for strictly alpha
    //and numeric digits here but
    //too lazy to do that right now
    $decval=0;
    $len=strlen($base36);

    for($i=0;$i<$len;$i++)
    {
      $digval = base36::myord($base36{$i});
      $digpower = $len-$i-1;
      $decval += (pow(36,$digpower)*$digval);
    }

    return $decval;
  }

  public static function mychr($ordinal){
    if($ordinal>=10){
      return chr(55+$ordinal);
    } else if($ordinal>=0){
      return $ordinal;
    } else return 0;
  }

  public static function myord($character){
    if(is_numeric($character)){
      return (int) $character;
    } else {
      return ord($character)-55;
    }
  }
}

and Base 26

class base26{
  public static function from10($base10){
   // $base10 stores a value in base 10
   // solution will have digits in an array

   if (!is_numeric($base10)){
      return 'A';
      //if this were python I'da thrown an exception
      //but this is PHP #SMH
   }
   if ($base10>2147483647){
      return 'A';
      //same excuse as above
   }

   $stringrep='';
   $index = 0 ;
   while ( $base10 > 0 )
    {
       $remainder = $base10 % 26 ;
       $base10       = (int) ($base10 / 26) ;  // integer division
       $stringrep =  chr($remainder+65) . $stringrep;
       $index ++ ;
    }

    if ( $stringrep==''){
      return 'A';
    }

    return $stringrep;
  }

  public static function to10($base26){
    //only upper case, unless we'd like to do a base 62!
    $base26 = strtoupper($base26);

    //i'da loved to do thorough check for strictly alpha
    //digits here but...
    //too lazy to do that right now
    $decval=0;
    $len=strlen($base26);

    for($i=0;$i<$len;$i++)
    {
      $digval = ord($base26{$i})-65;
      $digpower = $len-$i-1;
      $decval += (pow(26,$digpower)*$digval);
    }

    return $decval;
  }
}

Usage:

$mycode = base36::from10($anumber);
$mynumber = base36::to10($code);

//$mycode now contains the base36 equivalent of $anumber
//$mynumber now contains the base36 equivalent of $acode

Download code here: base36.class.php.zip