FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Implied cast differs from explicit cast
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Implied cast differs from explicit cast [message #176667 is a reply to message #176662] Mon, 16 January 2012 09:04 Go to previous messageGo to previous message
Curtis Dyer is currently offline  Curtis Dyer
Messages: 34
Registered: January 2011
Karma:
Member
Arno Welzel <usenet(at)arnowelzel(dot)de> wrote:

> Jerry Stuckle, 2012-01-16 04:39:
>
>> On 1/15/2012 10:08 PM, lb wrote:
>>> jstucklex(at)attglobal(dot)net wrote:
>>>> ...
>>>> No, it is not. '0x12' is a hex number, not an integer.
>>>> (int)'0x12' converts this correctly to 0.

As Arno points out, the hex value *is* an integer (once converted
from a string, in this case).

>>> OK, but if (int)'0x12' is 0, why is '0x12'+0 == 18?
>>>
>>> Why are they different? Aren't they both doing 'string
>>> conversion to number'?
>>
>> As I said - '0x12' is a hex value, not an integer. You are
>> trying to convert it as if it were a string representation of
>> an integer. The implicit conversion recognizes it is a hex
>> value and uses the appropriate conversion.
>
> There is no difference between "hex value" and "integer" - both
> 0x12 and 18 are just different representations of the same
> value. The problem is that the explizit cast from string to int
> does not take this into account.

The problem, as far as I can tell, has to do with the implicit
behavior of the "(int)$str" cast. Its default behavior seems to be
identicle to:

/* operates on base ten values; 10 is the default base when the
* second parameter is omitted. */
intval($str, 10);

So, '0x12' is obviously then considered an invalid base ten
integer, thus the expression, "(int)'0x12'" yields 0.

If you want to convert a hex value, either use hexdec() or use:

intval($str, 16);

Note, however, the behavior of hexdec($str) and intval($str, 16)
are not identicle! In some cases, hexdec() may be preferable, as
it will attempt to represent values too large for integers as
floating-point values. It also disregards non-hex value
characters in the string.

RTM here:

<http://php.net/intval>
<http://php.net/hexdec>

--
Curtis Dyer
<?$x='<?$x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Open Source Development
Next Topic: error from phpadmin
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 14:54:57 GMT 2024

Total time taken to generate the page: 0.03794 seconds