Issues on cast / convert to SHORT type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Julianz
Forum Newbie
Posts: 4
Joined: Fri Sep 25, 2009 7:55 am

Issues on cast / convert to SHORT type

Post 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
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Issues on cast / convert to SHORT type

Post 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;
}
 
Julianz
Forum Newbie
Posts: 4
Joined: Fri Sep 25, 2009 7:55 am

Re: Issues on cast / convert to SHORT type

Post by Julianz »

Wonderful!!!!! Thanks a lot!! :P :P :P
Julianz
Forum Newbie
Posts: 4
Joined: Fri Sep 25, 2009 7:55 am

Re: Issues on cast / convert to SHORT type

Post 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
Julianz
Forum Newbie
Posts: 4
Joined: Fri Sep 25, 2009 7:55 am

Re: Issues on cast / convert to SHORT type

Post 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)
Post Reply