in reply to why are hex values not numbers?

Here are 2 options. I favour the former.

#!/usr/bin/env perl use strict; use warnings; printf 'dec(%d) hex(%x)', 65, 0x41; print "\n"; printf 'dec(%d) hex(%x)', "65", eval "0x41"; print "\n";

Replies are listed 'Best First'.
Re^2: why are hex values not numbers?
by perl-diddler (Chaplain) on Sep 26, 2016 at 21:43 UTC
    But I think you missed the point.

    The "dec" isn't needed. That conversion happens without the explicit 'dec'.

    That's why I compared it against the shell equivalent, that makes no such differentiation. Perl *does* accept both decimal and hex outside of quotes, so why is the eval needed for hex numbers from strings? Seems a bit odd for a computer language.

      Because decimals alone are special inside string literals. perldata states:

      Hexadecimal, octal, or binary, representations in string literals (e.g. '0xff') are not automatically converted to their integer representation. The hex() and oct() functions make these conversions for you. See hex and oct for more details.

      Consider the decimal autoconversion as a bit of handy magic, nothing more.

        But it doesn't say "why". I don't see a reason ...?
      why is the eval needed for hex numbers from strings? Seems a bit odd for a computer language.

      In many (most) other languages -- I'd post a list, but the PM post size limit is something like 64kb -- you would have to explicitly convert the string containing a decimal number also -- see atoi(), atoi64(), atof() etc. in your local C compiler's CTR manual.

      Your expectations are naive; and your protestations more so.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Maybe you could just mention 1 mainstream language (besides perl) that doesn't accept hex or octal constants where it accepts decimal. You list examples of needing functions to convert hex and octal -- but those other languages also need functions for decimal. In the actual *language*, that the compiler reads -- they accept octal and hex where they accept binary.

        So instead of 64k, how about 1?

        BTW -- please note BrowserUK's tag line below -- this part:

        "Science is about questioning the status quo. Questioning authority".

        Seemed especially pertinent to some peoples' reactions to my asking "why is this this way" (with some answering, "that's the way it is" (status quo), or "as documented" (implicit acceptance of authority).

        Um, BrowserUK -- do you walk your taglines, or are they just decorative? :-)