note 107895 added to function.hexdec

From: Date: Tue, 13 Mar 2012 15:12:17 +0000
Subject: note 107895 added to function.hexdec
Groups: php.notes 
Request: Send a blank email to php-notes+get-186632@lists.php.net to get a copy of this message
This class is a simple example to understand how a hex-Value is transformed to a signed-Integer. Use hecDecBin::hex2bin($strHexValue). You can echo each step of the way to understand how it performs the conversion. This helps when you are faced with negative numbers that are hex-coded. This is purely for learning / debug purpose. Fast / unlimeted conversion work better with: http://de3.php.net/manual/en/function.hexdec.php#97172 <?php class hexDecBin { public static function invertBin($strBin) { $newBin=""; for($i=0;$i<=strlen($strBin);$i++) { if(substr($strBin,$i,1)==1) { $newBin.="0"; } else { $newBin.="1"; } } return $newBin; } public static function isNegative($strBin) { if(substr($strBin,0,1)=="1") { return true; } else { return false; } } public static function hexDec($strHex) { $binVal=self::hex2bin($strHex); if(self::isNegative($binVal)) { $binVal=self::invertBin($binVal); $sign=-1; } else { $sign=1; } $num=(bindec($binVal)*$sign); if($sign==-1) $num=$num-1; return $num; } public static function hex2bin($strHex) { $base=(strlen($strHex)*4); $decVal=hexdec($strHex); $numValue=$decVal; $binValue=decbin($numValue); $additionalBytes=($base-strlen($binValue)); for($i=0;$i<$additionalBytes;$i++) { $binValue="0" . $binValue; } return $binValue; } } ?> ---- Server IP: 87.106.1.137 Probable Submitter: 217.91.97.116 ---- Manual Page -- http://www.php.net/manual/en/function.hexdec.php Edit -- https://master.php.net/note/edit/107895 Del: integrated -- https://master.php.net/note/delete/107895/integrated Del: useless -- https://master.php.net/note/delete/107895/useless Del: bad code -- https://master.php.net/note/delete/107895/bad+code Del: spam -- https://master.php.net/note/delete/107895/spam Del: non-english -- https://master.php.net/note/delete/107895/non-english Del: in docs -- https://master.php.net/note/delete/107895/in+docs Del: other reasons-- https://master.php.net/note/delete/107895 Reject -- https://master.php.net/note/reject/107895 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186632) next »