KSC5601
[ class tree: KSC5601 ] [ index: KSC5601 ] [ all elements ]

Source for file Stream.php

Documentation is available at Stream.php

  1. <?php
  2. /**
  3.  * High level API for convert character set
  4.  *
  5.  * This api is high level api that convert between binary and numeric
  6.  * for convert character set with PHP
  7.  *
  8.  * @category   Charset
  9.  * @package    KSC5601
  10.  * @subpackage KSC5601_pure
  11.  * @author     JoungKyun.Kim <http://oops.org>
  12.  * @copyright  (c) 2015, JoungKyun.Kim
  13.  * @license    BSD License
  14.  * @version    $Id$
  15.  * @link       http://pear.oops.org/package/KSC5601
  16.  * @filesource
  17.  */
  18.  
  19. /**
  20.  * High level API for convert character set
  21.  *
  22.  * This api is high level api that convert between binary and numeric
  23.  * for convert character set with PHP
  24.  *
  25.  * @package KSC5601
  26.  */
  27. {
  28.     // {{{ function chr2hex ($c, $prefix = true, $dec = false)
  29.     /**
  30.      * Convert character to hex string
  31.      *
  32.      * @access public
  33.      * @return string  hexical strings or decimal strings
  34.      * @param  string  1 byte binary character
  35.      * @param  string  (optional) Defaults to true. Set ture, retuan with
  36.      *                  prefix '0x'
  37.      * @param  boolean (optional) Defaults to false. Set true, return with
  38.      *                  decimal strings.
  39.      */
  40.     static function chr2hex ($c$prefix true$dec false{
  41.         $prefix $prefix '0x' '';
  42.         if $dec === true )
  43.             $r ord ($c);
  44.         else {
  45.             $r strtoupper (dechex (ord ($c)));
  46.             /* big endian */
  47.             if strlen ($r)
  48.                 $r '0' $r;
  49.         }
  50.         return $prefix $r;
  51.     }
  52.     // }}}
  53.  
  54.     // {{{ function hex2chr ($c)
  55.     /**
  56.      * Convert hexical string to 1 byte binary character.
  57.      *
  58.      * @access public
  59.      * @return string 1 byte binary character
  60.      * @param  string hexical string
  61.      */
  62.     static function hex2chr ($c{
  63.         return chr (hexdec ($c));
  64.     }
  65.     // }}}
  66.  
  67.     // {{{ function chr2dec ($c)
  68.     /**
  69.      * Convert 1 byte binary character to decimal strings.
  70.      *
  71.      * @access public
  72.      * @return Decimal strings
  73.      * @param string  1 byte binary character
  74.      */
  75.     static function chr2dec ($c{
  76.         return ord ($c);
  77.     }
  78.     // }}}
  79.  
  80.     // {{{ function chr2bin ($c, $shift = '')
  81.     /**
  82.      * Convert binary character 1 byte to binary(numeric) strings
  83.      *
  84.      * @access public
  85.      * @return binary strings
  86.      * @param string  1 byte binary character
  87.      * @param string  (optional) Defaults to empty. shift string with '>> [N]' or '<< [N]'
  88.      */
  89.     static function chr2bin ($c$shift ''{
  90.         if preg_match ('/^(U\+|0x)/'$c) )
  91.             $c self::hex2chr ($c);
  92.  
  93.         $c ord ($c);
  94.  
  95.         if $shift && preg_match ('/^([<>]+)[\s]*([0-9]+)/'$shift$match) ) :
  96.             switch ($match[1]:
  97.                 case '>>' $c $c >> $match[2]break;
  98.                 case '<<' $c $c << $match[2]break;
  99.                 case '<'  $c $c <  $match[2]break;
  100.                 case '>'  $c $c >  $match[2]break;
  101.             endswitch;
  102.         endif;
  103.  
  104.         $c decbin ($c);
  105.         $l strlen ($c);
  106.         $prefix '';
  107.  
  108.         if $l :
  109.             $n $l;
  110.             for $i=0$i<$n$i++ :
  111.                 $prefix .= '0';
  112.             endfor;
  113.             $c $prefix $c;
  114.         endif;
  115.  
  116.         return $c;
  117.     }
  118.     // }}}
  119.  
  120.     // {{{ function bin2chr ($c)
  121.     /**
  122.      * Convert binary strings to 1byte binary character
  123.      *
  124.      * @access public
  125.      * @return string  1byte binary character
  126.      * @param  string  binary strings
  127.      */
  128.     static function bin2chr ($c{
  129.         return chr (bindec ($c));
  130.     }
  131.     // }}}
  132.  
  133.     // {{{ function check2byte ($byte)
  134.     /**
  135.      *
  136.      * @access public
  137.      * @param string $byte 
  138.      * @return 
  139.      */
  140.     static function check2byte ($byte{
  141.         return decbin (0x80 >> ($byte));
  142.     }
  143.     // }}}
  144.  
  145.     // {{{ function decbin ($s, $bit = 4)
  146.     /**
  147.      * Convert decimal strings to 4-digit binary(numeric) strings.
  148.      *
  149.      * @access public
  150.      * @return {$bit}-digit binary strings
  151.      * @param string  Given decimal strings
  152.      * @param numeric (optiona) Defaults to 4. number of digit.
  153.      */
  154.     static function decbin ($s$bit 4{
  155.         $r decbin ($s);
  156.         $l strlen ($r);
  157.  
  158.         if $l $bit )
  159.             $r sprintf ("%0{$bit}s"$r);
  160.  
  161.         return $r;
  162.     }
  163.     // }}}
  164.  
  165.     // {{{ function is_out_of_ksx1001 ($c1, $c2, $is_dec = false)
  166.     /**
  167.      * Check given 2byte is whether KSX1001 or out of range.
  168.      *
  169.      * @access public
  170.      * @return boolean When out of range, true.
  171.      * @param string  1st byte binary character
  172.      * @param string  2st byte binary character
  173.      * @param boolean (optional) Defaults to false. If type of 1st and 2st arguments
  174.      *                 is decimal, set true.
  175.      */
  176.     static function is_out_of_ksx1001 ($c1$c2$is_dec false{
  177.         if $c1 || $c2 )
  178.             return false;
  179.  
  180.         if $is_dec === false {
  181.             $c1 ord ($c1);
  182.             $c2 ord ($c2);
  183.         }
  184.  
  185.         if ( (($c1 0x80 && $c1 0xa1&& ($c2 0x40 && $c2 0x5b )) ||
  186.              (($c1 0x80 && $c1 0xa1&& ($c2 0x60 && $c2 0x7b )) ||
  187.              (($c1 0x80 && $c1 0xa1&& ($c2 0x80 && $c2 0xff )) ||
  188.              (($c1 0xa0 && $c1 0xc6&& ($c2 0x40 && $c2 0x5b )) ||
  189.              (($c1 0xa0 && $c1 0xc6&& ($c2 0x60 && $c2 0x7b )) ||
  190.              (($c1 0xa0 && $c1 0xc6&& ($c2 0x80 && $c2 0xa1 )) ||
  191.              ($c1 == 0xc6 && ($c2 0x40 && $k 0x53)) ) {
  192.             return true;
  193.         }
  194.  
  195.         return false;
  196.     }
  197.     // }}}
  198.  
  199.     // {{{ function execute_time ($t1, $t2)
  200.     /**
  201.      * Print execute time
  202.      *
  203.      * @access public
  204.      * @return string 
  205.      * @param array microtime() of starting
  206.      * @param array microtime() of ending
  207.      */
  208.     static function execute_time ($t1$t2{
  209.         $start explode (' '$t1);
  210.         $end   explode (' '$t2);
  211.  
  212.         return sprintf("%.2f"($end[1$end[0]($start[1$start[0]));
  213.     }
  214.     // }}}
  215. }
  216.  
  217. /*
  218.  * Local variables:
  219.  * tab-width: 4
  220.  * c-basic-offset: 4
  221.  * End:
  222.  * vim600: noet sw=4 ts=4 fdm=marker
  223.  * vim<600: noet sw=4 ts=4
  224.  */
  225. ?>

Documentation generated on Tue, 14 May 2019 02:00:45 +0900 by phpDocumentor 1.4.4