Page 1 of 1

Issues on cast / convert to SHORT type

Posted: Fri Sep 25, 2009 8:11 am
by Julianz
Hi everybody,
I'm Italian.. and I have a big problem :(

I have an application in Java and i'm trying to porting in PHP.
In this application, there is a short conversion like this:

A = (short)(B); the result is: 7528 = (short)(-252764824)

I need to do this in PHP.... but in PHP the short type does not exist.

... what can I do for calculate my 7528 .... started from -252764824??

Thank you all, and sorry for my bad english

Alan

Re: Issues on cast / convert to SHORT type

Posted: Fri Sep 25, 2009 8:19 am
by Mark Baker

Code: Select all

 
$A = short(-252764824);
echo $A;
 
function short($number) {
    $retVal = $number & 65535;
    if ($retVal >= 32768) {
        return - ($retVal - 32768);
    }
    return $retVal;
}
 

Re: Issues on cast / convert to SHORT type

Posted: Fri Sep 25, 2009 8:43 am
by Julianz
Wonderful!!!!! Thanks a lot!! :P :P :P

Re: Issues on cast / convert to SHORT type

Posted: Tue Oct 13, 2009 3:41 am
by Julianz
Sorry I have another question...
This function seems work correctly, but sometimes returns a wrong value

Example:

in Java (short)(1627237980) returns ---> -20900

in PHP with this function short(1627237980); returns ---> -11868 :cry:

any suggest....? :roll:
Thanks a lot :roll:
Alan

Re: Issues on cast / convert to SHORT type

Posted: Tue Oct 13, 2009 8:46 am
by Julianz
Edit:
I found a solution

This is the correct function 8) 8) 8) 8) 8)

Code: Select all

 
# $A = short(-252764824);
# echo $A;
#  
# function short($number) {
#     $retVal = $number & 65535;
#     if ($retVal >= 32768) {
#         return  ($retVal - 65536);
#     }
#     return $retVal;
# }
#  
that's all 8) 8) 8) 8)