D&C Lug - Home Page
Devon & Cornwall Linux Users' Group

[ Date Index ][ Thread Index ]
[ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG] Maths! not my strong point



OK,

Do I have a bit code that has taken me ages to get right.
In perl...

$binstr = '10011000111111001011100';
$dblnum=2**5;

for ($i =1; $i <= length($binstr); $i++)
{
 $a = substr($binstr, $i-1, 1);
 $num = 2**(5-$i);
 if ($a == 1)
 {
   $dblnum=$dblnum+$num;
 }
}
print "\$dblnum=$dblnum\n";


$dblnum=51.1233978271484


With rounding the answer is 51.1234

What I need to do is take 51.1234 and turn in back into a binary string.

(This is actually taking 4 hex values that are a float and turning them into a value, so the hex values is actually 844C7E5C which = 51.1234, need to go the other way, 51.1234 -> 844C7E5C)

--
'ooroo

Mike...(:)-)
---------------------------------------------------
Email: mike@xxxxxxxxxxxxx        o
You need only two tools.        o /////
A hammer and duct tape. If it    /@   `\  /) ~
doesn't move and it should use  >  (O)  X<  ~  Fish!!
the hammer. If it moves and      `\___/'  \) ~
shouldn't, use the tape.           \\\
---------------------------------------------------

--
The Mailing List for the Devon & Cornwall LUG
Mail majordomo@xxxxxxxxxxxx with "unsubscribe list" in the
message body to unsubscribe.



Lynx friendly