I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?
Yes, String#hex does this with or without the “0x” prefix; e.g.
"0x123".hex -> yields 291
"123".hex -> ditto
Also… is there something also for binary? Octal?
For octal numbers, it’s String#oct:
"123".oct -> yields 83
I don’t think there is a built-in for converting from binary numbers,
but there is a sample implementation in Hal Fulton’s excellent book,
“The Ruby Way”.
Hope this helps,