note 102125 added to language.types.float
| From: | (info@nstiac.com) | Date: | Fri, 28 Jan 2011 10:48:58 +0000 |
| Subject: | note 102125 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-176408@lists.php.net to get a copy of this message | ||
Second function (HEX2FLOAT32n) converts Hexadecimal 8 digit strings to fixed decimals float numbers.
(complementary to FLOAT2HEX32n above)
<?
/** Complementary functions to CONVERT PHP FLOATING POINT NUMBERS or DECIMALS
* (IEEE 754 single-precision 32 bit) TO HEXADECIMAL AND BACK.
*
* @author NSTIAC (Jorge D. Baigorri Salas) <http://www.nstiac.com> <http://www.n2works.com>
* @created on 28/Jan/2010 / time spent: 2hours approx.
* @special thanks to Thomas Finley's article "Floating Point"
* <http://tfinley.net/notes/cps104/floating.html>
*
* These functions allow to convert any php floating point numbers with the
* notation 1.234 (or fixed point numbers) into their corresponding 8 digits
* hexadecimal notation. (i.e.- 1557897.40 -> 49BE2C4B <- 1557897.40) by
* disregarding their implicit format and treating them at will as either
* integer, decimals, binary numbers, hexadecimal values, or plain text.
*
**/
/** HEX2FLOAT32n
* (Convert 8 digit hexadecimal values to fixed decimals float numbers (single-precision 32bits)
* Accepts 8 digit hexadecimal values on a string (i.e.- F1A9B02C) and single integer to fix number
of decimals
* @usage:
* hex2float32n("c9be2c49",2); returns -> "-1557897.13"
**/
function hex2float32n($number,$nd) {
//Separate each hexadecimal digit
for ($i=0; $i<strlen($number); $i++) {
$hex[]=substr($number,$i,1);
}
//Convert each hexadecimal digit to integer
for ($i=0; $i<count($hex); $i++) {
$dec[]=hexdec($hex[$i]);
}
//Convert each decimal value to 4bit binary and join on a string
for ($i=0; $i<count($dec); $i++) {
$binfinal.=sprintf("%04d",decbin($dec[$i]));
}
//Get sign 1bit value
$sign=substr($binfinal,0,1);
//Get exponent 8bit value
$exp=substr($binfinal,1,8);
//Get mantissa 23bit value
$mantissa=substr($binfinal,9);
//Convert & adjunt binary exponent to integer
$exp=bindec($exp);
$exp-=127;
//Assign mantissa to pre-scientific binary notation
$scibin=$mantissa;
//Split $scibin into integral & fraction parts through exponent
$binint=substr($scibin,0,$exp);
$binpoint=substr($scibin,$exp);
//Convert integral binary part to decimal integer
$intnumber=bindec("1".$binint);
//Split each binary fractional digit
for ($i=0; $i<strlen($binpoint); $i++) {
$tmppoint[]=substr($binpoint,$i,1);
}
//Reverse order to work backwards
$tmppoint=array_reverse($tmppoint);
//Convert base 2 digits to decimal
$tpointnumber=number_format($tmppoint[0]/2,strlen($binpoint),'.','');
for ($i=1; $i<strlen($binpoint); $i++) {
$pointnumber=number_format($tpointnumber/2,strlen($binpoint),'.','');
$tpointnumber=$tmppoint[$i+1].substr($pointnumber,1);
}
//Join both decimal section to get final number
$floatfinal=$intnumber+$pointnumber;
//Convert to positive or negative based on binary sign bit
if ($sign==1) { $floatfinal=-$floatfinal; }
//Format float number to fixed decimals required
return number_format($floatfinal,$nd,'.','');
}
/** Using either function will return a value which can be used on the other function
* to return the original input. (Hope it helps =)
**/
?>
----
Server IP: 69.147.83.197
Probable Submitter: 201.160.4.233
----
Manual Page -- http://www.php.net/manual/en/language.types.float.php
Edit -- https://master.php.net/note/edit/102125
Del: integrated -- https://master.php.net/note/delete/102125/integrated
Del: useless -- https://master.php.net/note/delete/102125/useless
Del: bad code -- https://master.php.net/note/delete/102125/bad+code
Del: spam -- https://master.php.net/note/delete/102125/spam
Del: non-english -- https://master.php.net/note/delete/102125/non-english
Del: in docs -- https://master.php.net/note/delete/102125/in+docs
Del: other reasons-- https://master.php.net/note/delete/102125
Reject -- https://master.php.net/note/reject/102125
Search -- https://master.php.net/manage/user-notes.php