Jump to content

[SOLVED] Math On Large Base 10 & 16 Unsigned Numbers


websmoken

Recommended Posts

Howdy guys,

 

I'm trying to add the uniqid() base 16, to a large (15 - 18 bit) base 10 number, store both, later retrieving both numbers and subtracting.  Is this possible to do without the results going into scientific notation?

 

Websmoken

 

Ken

Without posting the 3rd party script, heres what I got.  Orig_Num & HexNum is from the script.  I added the other code.

 

<?php
$HexNum = "0";
$NewNum = "0";
$Orig_Num = "1234567890123456";//any 15-18 bit base 10 number (always positive) from script.
$HexNum = uniqid("");// This $HexNum is from script as well (pulled from database).
$MidDec = base_convert($HexNum,16,10);
$NewNum = $NewNum + ($NewNum + $MidDec);//Store This number back in database.
?>

 

I do the reverse to obtain Original Number:

 

<?php

/* $NewNum retrieved from database then $Mid_Dec is subtracted from it, giving you the original number.  */
$Orig_Num = "0";
$Mid_Dec = "0";
$Mid_Dec = base_convert($HexNum,16,10);//$HexNum retrieved from database.
$Orig_Num = $NewNum - $Mid_Dec;
?>

 

Hope this helps.  What I have works its just in scientific notation before it goes in database and after its retrived.

 

Websmoken (Dave)

After much experimenting, here's what I've come up with...

<?php
$HexNum = "0";
$NewNum = "0";
$Orig_Num = "1234567890123456";//any 15-18 bit base 10 number (always positive) from script.
$HexNum = uniqid("");// This $HexNum is from script as well (pulled from database).
$MidDec = base_convert($HexNum,16,10);
$NewNum = $NewNum + ($NewNum + $MidDec);
//
// the next lines are my solution
//
ob_start();
var_dump($NewNum);
$x = ob_get_clean();
$NewNum = str_replace('float(','',str_replace(')','',$x)); //Store This number back in database.
?>

Do something similar in the other code snippet.

 

Ken

Ken

 

I tried the code sniplet you provided,  a huge THANKS by the way for your help.  I then input the number "4444444444444444", in the database was the number "5675336579130000".  The output that appeared when I tried to retrieve the number was "4444444444440000".  Something with the first sniplet on the input side must somehow be rounding off the 1st 4 LSB's.  The hexnum was 45f7d6871e84b.

The 3rd party program is for PHP version 4.3.11 & MySQL version 4.0.  I'm running PHP5 & MySQL4 on localhost.  Don't know it this helps or not.

 

Thanks again,

        Dave

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.