# Implied cast differs from explicit cast

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
When converting some strings to int (or double), I am finding that
an implied cast get different results from explicit cast. This happens
when the string contains a hex representation. The implied cast converts
the hex value, but the explicit cast returns 0. The manual under "String
conversion to numbers" says nothing about it allowing hex values. This is
with PHP-5.3.9 and older.

Script:
<?php
\$s1 = '0x12';
\$t1 = \$s1 + 0;
echo "Implied cast t1=\$t1\n";
\$t2 = (int)\$s1 + 0;
echo "Explicit cast t2=\$t2\n";

Output:
Implied cast t1=18
Explicit cast t2=0

I think this is odd. A side problem is that is_numeric('0x12') is true, but
((int)'0x12') is 0. Does anyone think this a bug?

## Re: Implied cast differs from explicit cast

On 1/15/2012 8:10 PM, lb wrote:

No, it is not.  '0x12' is a hex number, not an integer.  (int)'0x12'
converts this correctly to 0.

If you want to convert a hex number to an integer, you need to use
hexdec(), which is what the implied conversion does.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

jstucklex@attglobal.net wrote:

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'?

## Re: Implied cast differs from explicit cast

On 1/15/2012 10:08 PM, lb wrote:

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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

Jerry Stuckle, 2012-01-16 04:39:

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.

Example:

<?php
\$a = 0x12;
\$b = 18;
print \$a-\$b;
?>

Result:

0

--
Arno Welzel
http://arnowelzel.de

## Re: Implied cast differs from explicit cast

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

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);?>

## Re: Implied cast differs from explicit cast

On 1/16/2012 2:42 AM, Arno Welzel wrote:

In C/C++ there is no difference.  However, in PHP there is.  The 'x' is
not a valid decimal character, so (int) '0x12' returns 0.

The correct function to use when converting hexadecimal strings is hexdec().

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

Am 16.01.2012 14:25, schrieb Jerry Stuckle:

--cut

The last two sentences sound correct to me, so far. Looks like you are trying to
think like PHP.

But this does not make things better here, because PHP (the Zend T_INT_CAST) is
just
half-witted here.

/Str.

## Re: Implied cast differs from explicit cast

lb wrote:

Looks like Jerry doesn't know what an integer is either.

A hex number of course *is* an integer, unless its a hex representation
of floating point.

Killfile the prat like I have.

Don't expect Jerry to have a clue

because its either an integer number or a non numeric string?

I have given up trying to second guess PHP in casting.

One keeps things very simple as with a 2 year old (or jerry) when using PHP.

It can understand 18

It can understand 'eighteeen'

Lord knows what it does with '18'

As for 0x12...man, you are pushing the boat out, there.

Stop expecting PHP to be more than it is, a quick hack that writes good
web pages faster than almost anything else.

If you want a language that has a rule set and sticks to it rigidly,
that's C.

## Re: Implied cast differs from explicit cast

On 1/16/2012 8:28 AM, The Natural Philosopher wrote:

0x12 is not an integer.  It is a hexadecimal value.

It can have the same bit pattern as an integer - but it can also have
the same bit pattern as a character (Ctrl-R, to be exact).

You're mixing C/C++ and PHP.  They are not the same language.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

Jerry Stuckle, 2012-01-16 14:45:

No, 0x12 (and *not* "0x12") is the hexadecimal *representation* of the
value 18. There is no thing like "hexadecimal value" and "0x12" is a
*string* and not a number at all.

In fact PHP only knows to types of numbers: integers and floating point.
There is no "hexadecimal" data type.

Which also applies to the value 18 - this can also be the value of a
character.

Example:

<?php
\$a = 0x12;

print \$a;
?>

Result:

Value of \$a: 18, type of \$a: integer

So you see 0x12 *is* an integer.

Of course "0x12" (including the quotes) is a string and *converting* the
string to a number may need attention to the representation of the
number within the string.

--
Arno Welzel
http://arnowelzel.de

## Re: Implied cast differs from explicit cast

Arno Welzel, 2012-01-17 06:59:

Sorry - the example was not complete:

<?php
\$a = 0x12;

\$a = intval(\$a, 16);

print 'Value of \$a: ' . \$a . ', type of \$a: ' . gettype(\$a);
?>

--
Arno Welzel
http://arnowelzel.de

## Re: Implied cast differs from explicit cast

On 1/17/2012 1:00 AM, Arno Welzel wrote:

Sure.  You explicitly converted the hexadecimal value to an integer.
What do you expect?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

On 1/17/2012 12:59 AM, Arno Welzel wrote:

No, it is a hexadecimal value.  It may be an integer, it may be a
character, it may be a set of flags... It does not have to be an integer.

Excuse me - there is.  See hexdec().

All it is is a representation of bits in a byte.  How it is used
determines the datatype.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

Jerry Stuckle, 2012-01-17 14:16:

<http://php.net/manual/en/function.hexdec.php

You don't know what a data type is? Do you?

See <http://www.php.net/manual/en/language.types.php and show me, where
the "hexadecimal data type" is explained.

m(

--
Arno Welzel
http://arnowelzel.de

## Re: Implied cast differs from explicit cast

Arno Welzel, 2012-01-19 13:36:

<http://www.php.net/manual/en/language.types.integer.php very carefully.

There is no thing as "conversion of hexadecimal values to an integer".

EOD for me.

--
Arno Welzel
http://arnowelzel.de

## Re: Implied cast differs from explicit cast

On 1/19/2012 7:39 AM, Arno Welzel wrote:

That's good.  Come on back when you learn to read.  '0x12' is not an
integer.

But then you've already proven you have no idea what you're talking
about.  All you're doing is making yourself look even stoopider.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

On 1/19/2012 7:36 AM, Arno Welzel wrote:

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Implied cast differs from explicit cast

Arno Welzel wrote:

No, it's Jerrykins.

You expect too much.......

.......Including the ability to say 'er yes, I was wrong' with grace, or
at all.

## Re: Implied cast differs from explicit cast

On 1/19/2012 10:01 AM, The Natural Philosopher wrote: