October 2006
Intermediate to advanced
888 pages
16h 55m
English
Obviously all integers are representable in any base, because they are all stored internally in binary. Further, we know that Ruby can deal with integer constants in any of the four commonly used bases. This means that if we are concerned about base conversions, we must be concerned with strings in some fashion.
If you are concerned with converting a string to an integer, that is covered in section 2.24, “Converting Strings to Numbers (Decimal and Otherwise).”
If you are concerned with converting numbers to strings, the simplest way is to use the to_s method with the optional base parameter. This naturally defaults to 10, but it does handle bases up to 36 (using all letters of the alphabet).
237.to_s(2) # "11101101" ...
Read now
Unlock full access