Skip to content

Group of useful classes to convert between numeral systems

Notifications You must be signed in to change notification settings

rafalswierczek/numeral-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple numeral system conversion

Supported systems are binary, hexagonal and decimal.

Installation:

composer require rafalswierczek/numeral-system

Possible methods are:

  1. toBin - converts data to binary:

��

  1. toBinString - converts data to binary string:

1010000011001111

  1. toHexString - converts data to hexagonal string:

A0CF

  1. toInt - converts data to positive integer:

41167

Usage:

// in this example initial value is binary but it can be any of four above
$binary = random_bytes(4);

// all possible methods are used:
$hexStringBC = BinConverter::toHexString($binary);
$binaryStringHSC = HexStringConverter::toBinString($hexStringBC);
$hexStringBSC = BinStringConverter::toHexString($binaryStringHSC);
$intHSC = HexStringConverter::toInt($hexStringBSC);
$hexStringIC = IntConverter::toHexString($intHSC);
$binaryHSC = HexStringConverter::toBin($hexStringIC);
$binaryBSC = BinStringConverter::toBin($binaryStringHSC);
$binaryIC = IntConverter::toBin($intHSC);
$binaryStringIC = IntConverter::toBinString($intHSC);
$binaryStringBC = BinConverter::toBinString($binaryHSC);
$intBC = BinConverter::toInt($binaryIC);
$intBSC = BinStringConverter::toInt($binaryStringIC);

print_r(($binaryHSC === $binaryBSC && $binaryHSC === $binaryIC ? 'OK' : 'ERROR') . ' | binary: HSC === BSC === IC');
echo "\n";

print_r(($binaryStringBC === $binaryStringHSC && $binaryStringBC === $binaryStringIC ? 'OK' : 'ERROR') . ' | binary string: BC === HSC === IC');
echo "\n";

print_r(($hexStringBC === $hexStringBSC && $hexStringBC === $hexStringIC ? 'OK' : 'ERROR') . ' | hex string: BC === BSC === IC');
echo "\n";

print_r(($intBSC === $intHSC && $intBSC === $intBC ? 'OK' : 'ERROR') . ' | int: BSC === HSC === BC');

About

Group of useful classes to convert between numeral systems

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages