Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: hexadecimal division

by Anonymous Monk
on Dec 16, 2017 at 00:50 UTC ( #1205634=note: print w/replies, xml ) Need Help??

in reply to hexadecimal division

In finite field theory, "divide" means "multiply by the inverse." But I really don't know what you're trying to accomplish or what answer you expected, so I don't know how to help you.

Replies are listed 'Best First'.
Re^2: hexadecimal division
by stevieb (Canon) on Dec 16, 2017 at 01:01 UTC
    "But I really don't know what you're trying to accomplish or what answer you expected"

    I'm going to go all smartass which I don't do normally, but I found this comment entertaining and enlightening relative to the situation at hand... "divide by zero" is the appropriate error message here :)

    No disrespect intended; your post is well qualified. I just found it accurate, appropriate but funny in context (it's Friday, and I enjoy good posts after work hours on Fridays)

Re^2: hexadecimal division
by holandes777 (Scribe) on Dec 16, 2017 at 11:53 UTC
    Apologies for not being clear. IN the last part: The first part (xor-ing) worked fine, as the result was 0x05, It is the next part, the division or 0x05 by 0x10 that failed as the result was 0x30 not 0x00. To me that is off because the int of 5 / 16 is 0 (the API says X`00`)
      First there is nothing like a "hexadecimal division" in Perl.

      Numbers are represented internally as integers or floats in binary format.

      You are only free to use different base systems for the notation, see perlnumber .

      But the division is always the same!

      > the division or 0x05 by 0x10 that failed as the result was 0x30 not 0x00


      DB<17> p 0x05/0x10 0.3125 DB<18> p int(0x05/0x10) 0

      But "\x10" is not a number it's a one-character string with the ASCII code 16 (= 0x10)

      DB<25> p int(0x05/"\x10") Illegal division by zero at (eval 33)[C:/Perl_64/lib/] l +ine 2.

      "\x{Hex}" is just a way to use chr inside interpolated strings!

      (See "escape sequences" in perlop#Quote-and-Quote-like-Operators for details.)

      Stop using ASCII codes in strings for hex-numbers!

      Perl is not C, it will ONLY try to treat a string which looks like a decimal number as it's look-a-like number, all others as number zero in numerical context.

      DB<31> p "05"/"16" # strings with decimal(!) numbers 0.3125

      Again Perl is not C and will NOT try to take the byte-code of a character as a number, (unless you use ord explicitly ) !

      "\x10" doesn't look like a decimal number, it doesn't even look like any number at all!

      It's just one character which has position 16 (=0x10) in the ASCII table.

      DB<27> p ord("\x10") 16 DB<28> p "\x10" &#9658; # not a number, hence 0

      (NB: &#9658; shows as ► on my display this is not a number, not even a character.

      It's the Data_Link_Escape character in original ASCII, some OS try to overload it with something more meaningful.)

      This can only succeed (somehow) if you use the ASCII-code of a number character.

      Only the 10 characters "0".."9" look like a digit in ASCII.

      DB<1> p ord("1") 49 DB<2> p ord("\x31") 49 DB<3> p 0 + "\x31" # string = "1" looks like number 1 DB<4> p 5 / "\x31\x36" # string = "16" looks like number 0.3125

      I hope it's clearer now, since we already had this discussion in one of your last questions!

      --> vexed by the hex!

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Wikisyntax for the Monastery

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1205634]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2020-10-24 09:12 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (242 votes). Check out past polls.