Jump to content

change negative number to positive or unsigned


swcarrel

Recommended Posts

Generating a unique confirmation number for an order -- using

$Transaction = date("Ymd") . $phone . $name;
$ConfirmationNumber=crc32($Transaction);

This generates a negative number. I want to insert an unsigned number into the MySQL database, and print it on a "thank you" page to the customer.

Be gentle -- I'm a REAL newbie...
Link to comment
Share on other sites

[code]
<?php
/**
* Function to compute the unsigned crc32 value.
* PHP crc32 function returns int which is signed, so in order to get the correct crc32 value
* we need to convert it to unsigned value.
*
* @param $str - String to compute the unsigned crc32 value.
* @return $var - Unsinged inter value.
*/
function computeUnsignedCRC32($str){
   sscanf(crc32($str), "%u", $var);
   return $var;
}
?>
[/code]
Link to comment
Share on other sites

[!--quoteo(post=370747:date=May 2 2006, 05:20 PM:name=litebearer)--][div class=\'quotetop\']QUOTE(litebearer @ May 2 2006, 05:20 PM) [snapback]370747[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hmmm, how about
[code]
$x = (-1);  // negative 1

$y = 123456; // another negative number

$y = $y * $x;[/code]viola

Lite...
[/quote]

Perfect! Works fine. Seems like there should be a harder, more complicated way, though

[!--quoteo(post=370747:date=May 2 2006, 05:20 PM:name=litebearer)--][div class=\'quotetop\']QUOTE(litebearer @ May 2 2006, 05:20 PM) [snapback]370747[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hmmm, how about
[code]
$x = (-1);  // negative 1

$y = 123456; // another negative number

$y = $y * $x;[/code]viola

Lite...
[/quote]


[!--quoteo(post=370753:date=May 2 2006, 05:45 PM:name=samindenver)--][div class=\'quotetop\']QUOTE(samindenver @ May 2 2006, 05:45 PM) [snapback]370753[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Perfect! Works fine. Seems like there should be a harder, more complicated way, though
[/quote]
Thanks, Lite!

[!--quoteo(post=370751:date=May 2 2006, 05:30 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 2 2006, 05:30 PM) [snapback]370751[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]
<?php
/**
* Function to compute the unsigned crc32 value.
* PHP crc32 function returns int which is signed, so in order to get the correct crc32 value
* we need to convert it to unsigned value.
*
* @param $str - String to compute the unsigned crc32 value.
* @return $var - Unsinged inter value.
*/
function computeUnsignedCRC32($str){
   sscanf(crc32($str), "%u", $var);
   return $var;
}
?>
[/code]
[/quote]

Thanks, Cray -- I'll play with this one, too, in case sometimes the value is positive...
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.