Hex to Decimal

Hex to decimal in Linux

Type a hex value and pick a command. The output shows what your terminal prints.

printf

Output:

The four main commands

printf "%d\n" 0xFF           # 255
echo $((16#FF))              # 255
echo "ibase=16; FF" | bc     # 255
perl -e 'print hex("FF")'    # 255

Every command above prints 255. printf and $(( )) stop at 64 bits, while bc has no limit. Type a value in the picture to see what each one prints, including the errors.

CommandNeeds a prefixAccepts lowercaseLargest value
printf "%d\n" 0xFFYes, 0xYes7FFFFFFFFFFFFFFF, then clamps with a warning
echo $((16#FF))No, uses 16#Yes7FFFFFFFFFFFFFFF, then wraps to negative
bc with ibase=16NoNo, prints 0 or a syntax errorNo limit
perl -e 'print hex("FF")'Optional 0xYesFFFFFFFFFFFFFFFF

printf "%d\n" FF fails with invalid number, because printf reads a hex value only when it starts with 0x. The 16# form is Bash syntax, and POSIX shells such as dash reject it. In dash, use $((0xFF)) or printf.

Convert a variable

Put the hex string after 16# inside $(( )), and Bash reads it as base 16.

h=1A2B
echo $((16#$h))              # 6699
echo $((0x$h))               # 6699

Strip an existing prefix with ${h#0x}, and check the input before converting. A bad digit makes Bash print an error and skip that command, and the rest of the script still runs.

h=0x1A2B
echo $((16#${h#0x}))         # 6699

h=G1
[[ $h =~ ^[0-9A-Fa-f]+$ ]] && echo $((16#$h)) || echo "not hex"   # not hex

A function keeps the conversion in one place:

hex2dec() { printf '%d\n' "0x${1#0x}"; }
hex2dec 1A2B      # 6699
hex2dec 0xFF      # 255

Convert many values

Loop over the lines of a file, or pipe the whole file through bc.

while read -r h; do printf "%s %d\n" "$h" "0x$h"; done < values.txt

For a file with one uppercase hex value per line, bc converts everything in one call:

{ echo "ibase=16"; cat values.txt; } | bc

For a hex column inside a log, gawk has strtonum:

gawk '{ print strtonum("0x" $2) }' log.txt

gawk stores numbers as floating point, so values above 2⁵³ lose precision: strtonum("0xFFFFFFFFFFFFFFFF") returns 18446744073709551616, not 18446744073709551615. The default awk on Ubuntu and Debian is mawk, which has no strtonum. To convert a list in a browser instead, use the bulk hex to decimal converter.

Read hex from binary data

xxd -r -p turns a hex string into bytes, and od prints those bytes as a decimal number.

echo -n "E8030000" | xxd -r -p | od -An -t u4                 # 1000
echo -n "1A2B"     | xxd -r -p | od -An -t u2 --endian=big    # 6699

od -t u4 reads 4 bytes as an unsigned integer in the byte order of the machine, which is little endian on x86 and most ARM systems. Add --endian=big to read the bytes in the order you typed them. Without it, the bytes 1A 2B read as 11,034, not 6,699. See little endian hex to decimal for the reversal method.

Limits and errors

Bash uses signed 64-bit integers, so large values wrap or clamp, and bad digits stop the command.

InputResultCause
echo $((16#7FFFFFFFFFFFFFFF))9223372036854775807Largest signed 64-bit value
echo $((16#8000000000000000))-9223372036854775808Top bit set, so the value reads as negative
echo $((16#FFFFFFFFFFFFFFFF))-1All 64 bits set
echo $((16#FFFFFFFFFFFFFFFFFF))-1Bash keeps the lowest 64 bits and drops the rest
printf "%d\n" 0xFFFFFFFFFFFFFFFF9223372036854775807printf clamps and prints a warning
printf "%u\n" 0xFFFFFFFFFFFFFFFF18446744073709551615%u reads the value as unsigned
echo $((16#G1))error: value too great for baseG is not a hex digit
echo "ibase=16; ff" | bc0bc reads lowercase letters as variables
echo $((010))8A leading 0 means octal in Bash arithmetic

Use bc or python3 -c 'print(int("FFFFFFFFFFFFFFFFFFFF", 16))' for anything past 64 bits. Convert lowercase input for bc with ${h^^} or tr a-f A-F. Set obase before ibase when you use both, because after ibase=16, the number 10 means sixteen.

Decimal to hex

printf with %x prints a decimal number as hex.

printf "%x\n" 255      # ff
printf "%X\n" 255      # FF
printf "%08X\n" 255    # 000000FF
printf "%#x\n" 255     # 0xff
echo "obase=16; 255" | bc    # FF

For the full reverse method, see the decimal to hex converter.

Frequently asked questions

How do you convert hex to decimal in Bash?
Use echo $((16#FF)) or printf "%d\n" 0xFF. Both print 255. The first takes the digits after 16#, and the second needs the 0x prefix.
How do you convert hex to decimal with bc?
Set the input base first: echo "ibase=16; FF" | bc prints 255. Write the digits in uppercase, because bc reads lowercase letters as variable names. A value such as ff prints 0, and a mix such as 1a gives a syntax error.
Why does printf say invalid number?
printf reads hex only with the 0x prefix. printf "%d\n" FF fails, and printf "%d\n" 0xFF prints 255.
What is the largest hex value Bash can convert?
7FFFFFFFFFFFFFFF, which is 9,223,372,036,854,775,807. Bash arithmetic wraps to negative values above it. Use bc or Python for longer values.
How do you convert a hex string stored in a variable?
Write echo $((16#$h)), where h holds the digits. Remove a 0x prefix first with ${h#0x}.