Overview

HTTPS SSH

Readme

Usage/Tests for OddBases

Set a value for use in the tests

/* **
 * Decimal value for tests
 */
$DecimalValue           = 123456;

Using the static class

/* **
 * Use the static class
 */
require "oddbases-static.php";

// Defaults to Base 32
OddBases::SetBase();
// Or use a different base
    //OddBases::SetBase(62);

// Straight encode
$BaseString             = OddBases::ToBase($DecimalValue);

// Pad string using default values.
$BaseStringWithPadding  = OddBases::ToBase($DecimalValue, true);

// Pad string using custom character, default length
$BSWPandCustomCharacter = OddBases::ToBase($DecimalValue, true, '-');

// Pad string with custom character and custom length
$BSWPACCToEightPlaces   = OddBases::ToBase($DecimalValue, true, '-', 8);

// Output
print "Using base " . OddBases::GetBase() . "\n";
print "----------\n";
print "Decimal value being encoded:\t$DecimalValue\n";
print "Becomes\t\t\t\t$BaseString\tReturn:\t" . OddBases::ToDecimal($BaseString) . "\n";
/*
 * As the values below are padded, they will fail the regular expression test that checks they are of the
 * base being used. In this case, false is passed for the value of `$WithCheck` and as they are not part
 * of the base, they return false rather than a character position.
 */
print "With padding\t\t\t$BaseStringWithPadding\tReturn:\t" . OddBases::ToDecimal($BaseStringWithPadding, false) . "\n";
print "Custom padding character\t$BSWPandCustomCharacter\tReturn:\t" . OddBases::ToDecimal($BSWPandCustomCharacter, false) . "\n";
print "Custom padding length\t\t$BSWPACCToEightPlaces\tReturn:\t" . OddBases::ToDecimal($BSWPACCToEightPlaces, false) . "\n";

Using the non-static class

require "oddbases-non-static.php";

// Initialise with default base
$ob = new OddBases();
// Initialise with custom base
$ob1 = new OddBases(24);

print "Using base... \n";
print "\$ob is using base '" . $ob->getBase() . "'\n";
print "\$ob1 is using base '" . $ob1->getBase() . "'\n";
print "----------\n";
print "Decimal value being encoded:\t$DecimalValue\n";

// Straight encode
$obBaseString = $ob->toBase($DecimalValue);
$ob1BaseString = $ob1->toBase($DecimalValue);

// Pad with default values
$obBaseWithPadding = $ob->toBase($DecimalValue, true);
$ob1BaseWithPadding = $ob1->toBase($DecimalValue, true);

// Pad with custom character
$obBasePadCustomChar = $ob->toBase($DecimalValue, true, '=');
$ob1BasePadCustomChar = $ob1->toBase($DecimalValue, true, '=');

// Pad with custom character and length
$obBasePadEightPlaces = $ob->toBase($DecimalValue, true, '=', 8);
$ob1BasePadEightPlaces = $ob1->toBase($DecimalValue, true, '=', 8);

print "Becomes (using \$ob)\t$obBaseString\t[Return: " . $ob->toDecimal($obBaseString) . "]\n";
print "Becomes (using \$ob1)\t$ob1BaseString\t[Return: " . $ob1->toDecimal($ob1BaseString) . "]\n";

/*
 * Padded values
 */
print "With padding (\$ob) becomes\t$obBaseWithPadding\t[Return: " . $ob->toDecimal($obBaseWithPadding, false) . "]\n";
print "With padding (\$ob1) becomes\t$ob1BaseWithPadding\t[Return: " . $ob1->toDecimal($ob1BaseWithPadding, false) . "]\n";
print "With custom character (\$ob) becomes\t$obBasePadCustomChar\t[Return: " . $ob->toDecimal($obBasePadCustomChar, false) . "]\n";
print "With custom character (\$ob1) becomes\t$ob1BasePadCustomChar\t[Return: " . $ob1->toDecimal($ob1BasePadCustomChar, false) . "]\n";
print "With custom character and length (\$ob) becomes\t$obBasePadEightPlaces\t[Return: " . $ob->toDecimal($obBasePadEightPlaces, false) . "]\n";
print "With custom character and length (\$ob1) becomes\t$ob1BasePadEightPlaces\t[Return: " . $ob1->toDecimal($ob1BasePadEightPlaces, false) . "]\n";

License

"CC0 Public Domain" https://creativecommons.org/publicdomain/zero/1.0/