# File: cifra.php

 Recommend this page to a friend! ReTweet
 Classes of zandor > Cifra > cifra.php > Download
File: cifra.php Class source text/plain class code Cifra Convert numbers between representation formats By zandor none 11 years ago 8,680 bytes

## Contents

 ``` "",                         "1" => "I",                         "2" => "II",                         "3" => "III",                         "4" => "IV",                         "5" => "V",                         "6" => "VI",                         "7" => "VII",                         "8" => "VIII",                         "9" => "IX" ) ; \$twoDIGITarray = array( "0" => "",                         "1" => "X",                         "2" => "XX",                         "3" => "XXX",                         "4" => "XL",                         "5" => "L",                         "6" => "LX",                         "7" => "LXX",                         "8" => "LXXX",                         "9" => "XC" ) ; \$threeDIGITarray = array( "0" => "",                           "1" => "C",                           "2" => "CC",                           "3" => "CCC",                           "4" => "CD",                           "5" => "D",                           "6" => "DC",                           "7" => "DCC",                           "8" => "DCCC",                           "9" => "CM" ) ; \$fourDIGITarray = array( "0" => "",                          "1" => "M",                          "2" => "MM",                          "3" => "MMM" ) ; \$ALPHANUMERICALarray = array( 0 => "0",                             1 => "1",                             2 => "2",                             3 => "3",                             4 => "4",                             5 => "5",                             6 => "6",                             7 => "7",                             8 => "8",                             9 => "9",                             10 => "A",                             11 => "B",                             12 => "C",                             13 => "D",                             14 => "E",                             15 => "F"                           ) ; class cifra {     function cifra()     {         \$this->arabic = "" ;         \$this->roman = "" ;     }     function set_arabic( \$a ) { \$this->arabic = \$a ; }     function pack( \$a )     {         \$packing_zeros = 4 - ( strlen( \$a ) % 4 ) ;         if ( \$packing_zeros == 4 ) return \$a ;                 for ( \$i = 0 ; \$i < \$packing_zeros; \$i++ ) \$a = "0\$a" ;                 return \$a ;     }     function findstr( \$strIn, \$strQuery )     {         \$length_in = strlen( \$strIn );         \$length_qry = strlen( \$strQuery );             for( \$i = 0 ; \$i <= ( \$length_in - \$length_qry ); \$i++ )         {               \$strOut = substr( \$strIn, \$i, \$length_qry );               if ( strcmp( \$strQuery, \$strOut ) == 0 ) return true ;         }             return false ;     }         //////////////////////////////////////////////////////     function get_arabic() { return \$this->arabic ; }     function get_roman() { return \$this->roman ; }     function format_number( \$num, \$base ) { return "\$num\$base" ; }     function to_roman( \$a )     {         \$this->set_arabic( \$a );         \$a = \$this->pack( \$a ) ;                 if ( \$a >= 4000 ) return "out of range" ;                 \$inputDIGIT = "\$a" ;         \$inputLEN = strlen( \$inputDIGIT );                 \$a = array();                 for ( \$i = 0 ; \$i < \$inputLEN; \$i++ )         {             \$digit = \$inputDIGIT{\$i} ;                         switch( \$i )             {                 case 3:                   \$a = \$GLOBALS['oneDIGITarray'] ;                 break;                 case 2:                   \$a = \$GLOBALS['twoDIGITarray'] ;                 break;                 case 1:                   \$a = \$GLOBALS['threeDIGITarray'] ;                 break;                 case 0:                   \$a = \$GLOBALS['fourDIGITarray'] ;                 break;                 default:                 return "-1";                 break;             }                         \$this->roman .= \$a["\$digit"] ;         }                 return \$this->roman ;     }     function to_arabic( \$a )     {         \$sum = 0 ;         ////////////////////////////////////////////////         \$i = 0 ;         \$c = 0 ;         \$digitARRAY = \$GLOBALS['fourDIGITarray'] ;         \$token = "";         foreach( \$digitARRAY as \$value )         {             if ( \$this->findstr( \$a, \$value ) === true && strlen( \$value ) >= strlen( \$token ) )             {                 \$c = \$i ;                 \$token = \$value ;             }             \$i++ ;         }                 \$s = \$c * pow( 10, 3 ) ;         \$sum += \$s ;         \$a = substr( \$a, strlen( \$token ) );         ////////////////////////////////////////////////         \$i = 0 ;         \$c = 0 ;         \$digitARRAY = \$GLOBALS['threeDIGITarray'] ;         \$token = "";         foreach( \$digitARRAY as \$value )         {             if ( \$this->findstr( \$a, \$value ) === true && strlen( \$value ) >= strlen( \$token ) )             {                 \$c = \$i ;                 \$token = \$value ;             }             \$i++ ;         }                 \$s = \$c * pow( 10, 2 ) ;         \$sum += \$s ;         \$a = substr( \$a, strlen( \$token ) );         ////////////////////////////////////////////////         \$i = 0 ;         \$c = 0 ;         \$digitARRAY = \$GLOBALS['twoDIGITarray'] ;         \$token = "";         foreach( \$digitARRAY as \$value )         {             if ( \$this->findstr( \$a, \$value ) === true && strlen( \$value ) >= strlen( \$token ) )             {                 \$c = \$i ;                 \$token = \$value ;             }             \$i++ ;         }                 \$s = \$c * pow( 10, 1 ) ;         \$sum += \$s ;         \$a = substr( \$a, strlen( \$token ) );         ////////////////////////////////////////////////         \$i = 0 ;         \$c = 0 ;         \$digitARRAY = \$GLOBALS['oneDIGITarray'] ;         \$token = "";         foreach( \$digitARRAY as \$value )         {             if ( strcmp( \$a, \$value ) == 0 && strlen( \$value ) >= strlen( \$token ) )             {                 \$c = \$i ;                 \$token = \$value ;             }             \$i++ ;         }                 \$s = \$c * pow( 10, 0 ) ;         \$sum += \$s ;         \$a = substr( \$a, strlen( \$token ) );         ////////////////////////////////////////////////         \$this->arabic = \$sum ;         return \$sum ;     }         function to_base( \$inputNUM, \$base )     {           if ( \$base < 2 || \$base > 16 ) return "NO BASE ALLOWED !" ;                     \$ret_str = "" ;                     \$ALPHANUMarray = \$GLOBALS['ALPHANUMERICALarray'] ;                     while( \$inputNUM >= \$base )           {                 \$remainder = \$inputNUM % \$base ;                 \$digit = \$ALPHANUMarray[\$remainder];                 \$ret_str = "\$digit" . \$ret_str ;                 \$inputNUM = ( \$inputNUM - \$remainder ) / \$base ;           }               \$digit = \$ALPHANUMarray[\$inputNUM];           \$ret_str = "\$digit" . \$ret_str ;                     \$ret_str = \$this->pack( \$ret_str ) ;                     return \$ret_str ;     }     function to_decimal( \$inputNUM, \$base )     {         \$sum = 0 ;         \$len = strlen( \$inputNUM );                 for ( \$i = \$len - 1; \$i >= 0 ; \$i-- )         {             \$c = \$inputNUM{\$i} + 0;             \$exponent = \$len - 1 - \$i ;             \$sum += \$c * pow( \$base, \$exponent );         }                 return \$sum ;     }             ///////////// CLASS MEMBERS //////////////////////////         var \$arabic ;     var \$roman ; } ?> ```