Convert hex to decimal in Ruby

A practical review of converting hexadecimal text to decimal in Ruby: suitability, what the character rewrite changes, and how the result behaves when copied or shared.

Is this transformation suitable for your intended use?

Converting hexadecimal text to decimal in Ruby is a good fit when the hex string stands for an integer you need to compute with, compare, store as a number, or show in base 10. The main benefit is a single Integer value whose magnitude is independent of spelling. The decisive tradeoff is that the compact hex form disappears, along with the nibble-by-nibble layout people use for colors, masks, and addresses. Keep the original hex spelling when that spelling is the canonical record.

  • Use the conversion when you need arithmetic, numeric comparison, or a base-10 display of the same integer.
  • Keep hex as the working text for CSS colors, memory addresses, content hashes, bit masks, and protocol constants that must match a hex-based spec.
  • Keep both spellings when some readers search by hex and others expect a decimal number.

What this conversion actually changes

This is a radix rewrite of stored characters. Digits 0–9 and letters A–F (either case) are read as a base-16 integer; that same integer is then written with decimal digits 0–9. The character sequence in the text changes. Hex 10 becomes decimal 16; hex FF becomes decimal 255. Letter digits are replaced by decimal digit strings. Digit order follows place value in the new base.

The change lives in the stored string or Integer, which sets it apart from mirrored-glyph fonts, Unicode lookalike substitution, CSS presentation, and bidirectional display tricks. Those techniques alter appearance while leaving the original characters in place. “Reverse” and “mirror” do not describe this workflow: the digits are not flipped or visually reflected. “Font” is also a mismatch; output uses ordinary decimal digits.

A well-formed hex integer is an optional sign, an optional base prefix such as 0x, and the alphabet 0–9 and A–F. Combining marks and multi-code-point emoji sit outside that alphabet. Strict parsers reject them; a permissive parser may stop at the first unexpected character. No sample input/output pair was supplied with this brief, so the mappings below are the standard hex–decimal relationship the rewrite produces.

Hex textDecimal textInteger value
000
A1010
101616
FF255255
100256256

In Ruby the usual tools are String#to_i with base 16 and Kernel#Integer with an explicit base. Both yield an Integer. to_i(16) reads a leading run of hex digits and treats a string that does not start with a hex digit as 0. Integer with a base checks the whole string and raises ArgumentError on malformed input. Prefix handling differs: a 0x prefix is Ruby’s hex literal form, and each method has its own rule for whether that prefix is allowed. Confirm whether your source text is bare hex digits or a prefixed literal before choosing a method. Ruby Integer values have arbitrary precision, so a long hex digit string still converts to an exact integer. Turning the Integer back into text for display uses to_s (decimal by default) or formatted interpolation.

hex = "FF"
value = hex.to_i(16)
value.to_s
# 255

Integer("ff", 16)
# 255

Integer("-10", 16)
# -16

Will the result remain readable and usable after copying or sharing?

The decimal spelling uses ASCII digits 0–9 and, for negatives, an ASCII minus. Those characters copy and paste as ordinary text and stay editable in typical editors, terminals, and forms. A screen reader will generally speak them as a number. Search, diff, and exact-match lookup look for the decimal spelling; they will miss the original hex string unless you also keep that original. If the hex form is the identifier other people will search for, retain it beside the decimal value.

A plain-text decimal equivalent is the right shareable form when the audience needs a number they can read, paste into a calculator, or use in further Ruby code. The original hex string remains the right shareable form when the audience must match a spec that is written in hex. This review does not include a scoped test of a named screen reader, browser, or destination app. Ordinary ASCII digits are widely supported, and behavior in any specific tool is unverified here.