source-class-Com.Tecnick.File.Byte

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Byte.php
  4:  *
  5:  * @since       2015-07-28
  6:  * @category    Library
  7:  * @package     File
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-file
 12:  *
 13:  * This file is part of tc-lib-file software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\File;
 17: 
 18: /**
 19:  * Com\Tecnick\File\Byte
 20:  *
 21:  * Function to read byte-level data
 22:  *
 23:  * @since       2015-07-28
 24:  * @category    Library
 25:  * @package     File
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-file
 30:  */
 31: class Byte
 32: {
 33:     /**
 34:      * String to process
 35:      *
 36:      * @var string
 37:      */
 38:     protected $str = '';
 39: 
 40:     /**
 41:      * Initialize a new string to be processed
 42:      *
 43:      * @param string $str String from where to extract values
 44:      */
 45:     public function __construct($str)
 46:     {
 47:         $this->str = $str;
 48:     }
 49: 
 50:     /**
 51:      * Get BYTE from string (8-bit unsigned integer).
 52:      *
 53:      * @param int $offset Point from where to read the data.
 54:      *
 55:      * @return int 8 bit value
 56:      *
 57:      */
 58:     public function getByte($offset)
 59:     {
 60:         $val = unpack('Ci', substr($this->str, $offset, 1));
 61:         return $val['i'];
 62:     }
 63: 
 64:     /**
 65:      * Get ULONG from string (Big Endian 32-bit unsigned integer).
 66:      *
 67:      * @param int $offset Point from where to read the data
 68:      *
 69:      * @return int 32 bit value
 70:      */
 71:     public function getULong($offset)
 72:     {
 73:         $val = unpack('Ni', substr($this->str, $offset, 4));
 74:         return $val['i'];
 75:     }
 76: 
 77:     /**
 78:      * Get USHORT from string (Big Endian 16-bit unsigned integer).
 79:      *
 80:      * @param int $offset Point from where to read the data
 81:      *
 82:      * @return int 16 bit value
 83:      */
 84:     public function getUShort($offset)
 85:     {
 86:         $val = unpack('ni', substr($this->str, $offset, 2));
 87:         return $val['i'];
 88:     }
 89: 
 90:     /**
 91:      * Get SHORT from string (Big Endian 16-bit signed integer).
 92:      *
 93:      * @param int $offset Point from where to read the data.
 94:      *
 95:      * @return int 16 bit value
 96:      */
 97:     public function getShort($offset)
 98:     {
 99:         $val = unpack('si', substr($this->str, $offset, 2));
100:         return $val['i'];
101:     }
102: 
103:     /**
104:      * Get UFWORD from string (Big Endian 16-bit unsigned integer).
105:      *
106:      * @param int $offset Point from where to read the data.
107:      *
108:      * @return int 16 bit value
109:      */
110:     public function getUFWord($offset)
111:     {
112:         return $this->getUShort($offset);
113:     }
114: 
115:     /**
116:      * Get FWORD from string (Big Endian 16-bit signed integer).
117:      *
118:      * @param int $offset Point from where to read the data.
119:      *
120:      * @return int 16 bit value
121:      */
122:     public function getFWord($offset)
123:     {
124:         $val = $this->getUShort($offset);
125:         if ($val > 0x7fff) {
126:             $val -= 0x10000;
127:         }
128:         return $val;
129:     }
130: 
131:     /**
132:      * Get FIXED from string (32-bit signed fixed-point number (16.16).
133:      *
134:      * @param int $offset Point from where to read the data.
135:      *
136:      * @return int 16 bit value
137:      *
138:      */
139:     public function getFixed($offset)
140:     {
141:         // mantissa.fraction
142:         return (float)($this->getFWord($offset).'.'.$this->getUShort($offset + 2));
143:     }
144: }
145: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy