Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4477

Re: Implied cast differs from explicit cast

From Curtis Dyer <dyer85@gmail.com>
Newsgroups comp.lang.php
Subject Re: Implied cast differs from explicit cast
Date 2012-01-16 09:04 +0000
Organization A noiseless patient Spider
Message-ID <jf0p64$mil$2@dont-email.me> (permalink)
References <4f1378ee$0$16741$882e7ee2@usenet-news.net> <jevvqe$1gk$1@dont-email.me> <4f139498$0$16818$882e7ee2@usenet-news.net> <jf066c$qig$1@dont-email.me> <4F13D4E3.3010208@arnowelzel.de>

Show all headers | View raw


Arno Welzel <usenet@arnowelzel.de> wrote:

> Jerry Stuckle, 2012-01-16 04:39:
> 
>> On 1/15/2012 10:08 PM, lb wrote:
>>> jstucklex@attglobal.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);?>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Implied cast differs from explicit cast lb <lb2048@pobox.com.delete> - 2012-01-16 01:10 +0000
  Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-15 20:51 -0500
    Re: Implied cast differs from explicit cast lb <lb2048@pobox.com.delete> - 2012-01-16 03:08 +0000
      Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-15 22:39 -0500
        Re: Implied cast differs from explicit cast Arno Welzel <usenet@arnowelzel.de> - 2012-01-16 08:42 +0100
          Re: Implied cast differs from explicit cast Curtis Dyer <dyer85@gmail.com> - 2012-01-16 09:04 +0000
          Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-16 08:25 -0500
            Re: Implied cast differs from explicit cast "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-16 19:51 +0100
      Re: Implied cast differs from explicit cast The Natural Philosopher <tnp@invalid.invalid> - 2012-01-16 13:28 +0000
        Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-16 08:45 -0500
          Re: Implied cast differs from explicit cast Arno Welzel <usenet@arnowelzel.de> - 2012-01-17 06:59 +0100
            Re: Implied cast differs from explicit cast Arno Welzel <usenet@arnowelzel.de> - 2012-01-17 07:00 +0100
              Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-17 08:17 -0500
            Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-17 08:16 -0500
              Re: Implied cast differs from explicit cast Arno Welzel <usenet@arnowelzel.de> - 2012-01-19 13:36 +0100
                Re: Implied cast differs from explicit cast Arno Welzel <usenet@arnowelzel.de> - 2012-01-19 13:39 +0100
                Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-19 09:06 -0500
                Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-19 09:05 -0500
                Re: Implied cast differs from explicit cast The Natural Philosopher <tnp@invalid.invalid> - 2012-01-19 15:01 +0000
                Re: Implied cast differs from explicit cast Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-19 14:41 -0500
  Re: Implied cast differs from explicit cast "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-16 09:49 +0100
  Re: Implied cast differs from explicit cast "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-01-16 11:48 +0100
    Re: Implied cast differs from explicit cast lb <lb2048@pobox.com.delete> - 2012-01-17 02:35 +0000

csiph-web