Karol Mazurek
Karol Mazurek

Reputation: 37

Conversion from hex to decimal in ruby

0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFE_FFFFFC2F.ord => 115792089237316195423570985008687907853269984665640564039457584007908834671663

0xFFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFF_FFFFFFFE_FFFFFC2F.to_i => 115792089237316195423570985008687907853269984665640564039457584007908834671663

Could somebody explain me why both of these methods return the same integer value? I'm not sure that i understand the method ord...

Returns the codepoint of the first character of the string, assuming a single-byte character encoding"

I found that, but here I've go a hex value and i used the method then get a decimal value, so "What the hex?" :D

PS: Is it decimal value for sure or am I wrong?

Upvotes: 1

Views: 337

Answers (1)

mrzasa
mrzasa

Reputation: 23317

You've quoted String#ord and here you call #ord on Integer - this value is not decimal, nor string, but integer.

Integer#ord docs:

ord → self

Returns the int itself.

97.ord #=> 97 This method is intended for compatibility to character literals in Ruby 1.9.

Upvotes: 1

Related Questions