Created
November 22, 2014 15:37
-
-
Save rubensayshi/aa169f77c4a0cecdc1f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace BlockTrail\Util; | |
| class HexParser { | |
| protected $position = 0; | |
| protected $string = ''; | |
| public function __construct($string) { | |
| $this->string = $string; | |
| } | |
| public function getString() { | |
| return $this->string; | |
| } | |
| public function getCount() { | |
| return strlen($this->string) / 2; | |
| } | |
| public function read($count) { | |
| $count = $count * 2; | |
| $this->position += $count; | |
| return substr($this->string, $this->position - $count, $count); | |
| } | |
| public function readDecimal($count) { | |
| return hexdec($this->read($count)); | |
| } | |
| public function readString($count) { | |
| return self::hexToStr($this->read($count)); | |
| } | |
| public function getPosition() { | |
| return $this->position; | |
| } | |
| public static function hexToStr($hex) { | |
| $string = ''; | |
| for ($i = 0; $i < strlen($hex) - 1; $i += 2) { | |
| $string .= chr(hexdec($hex[$i] . $hex[$i + 1])); | |
| } | |
| return $string; | |
| } | |
| public static function strToHex($string) { | |
| $hex = ''; | |
| for ($i = 0; $i < strlen($string); $i++) { | |
| $hex .= dechex(ord($string[$i])); | |
| } | |
| return $hex; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment