The four main commands
printf "%d\n" 0xFF # 255
echo $((16#FF)) # 255
echo "ibase=16; FF" | bc # 255
perl -e 'print hex("FF")' # 255Every 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.
| Command | Needs a prefix | Accepts lowercase | Largest value |
|---|---|---|---|
printf "%d\n" 0xFF | Yes, 0x | Yes | 7FFFFFFFFFFFFFFF, then clamps with a warning |
echo $((16#FF)) | No, uses 16# | Yes | 7FFFFFFFFFFFFFFF, then wraps to negative |
bc with ibase=16 | No | No, prints 0 or a syntax error | No limit |
perl -e 'print hex("FF")' | Optional 0x | Yes | FFFFFFFFFFFFFFFF |
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)) # 6699Strip 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 hexA function keeps the conversion in one place:
hex2dec() { printf '%d\n' "0x${1#0x}"; }
hex2dec 1A2B # 6699
hex2dec 0xFF # 255Convert 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.txtFor a file with one uppercase hex value per line, bc converts everything in one call:
{ echo "ibase=16"; cat values.txt; } | bcFor a hex column inside a log, gawk has strtonum:
gawk '{ print strtonum("0x" $2) }' log.txtgawk 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 # 6699od -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.
| Input | Result | Cause |
|---|---|---|
echo $((16#7FFFFFFFFFFFFFFF)) | 9223372036854775807 | Largest signed 64-bit value |
echo $((16#8000000000000000)) | -9223372036854775808 | Top bit set, so the value reads as negative |
echo $((16#FFFFFFFFFFFFFFFF)) | -1 | All 64 bits set |
echo $((16#FFFFFFFFFFFFFFFFFF)) | -1 | Bash keeps the lowest 64 bits and drops the rest |
printf "%d\n" 0xFFFFFFFFFFFFFFFF | 9223372036854775807 | printf clamps and prints a warning |
printf "%u\n" 0xFFFFFFFFFFFFFFFF | 18446744073709551615 | %u reads the value as unsigned |
echo $((16#G1)) | error: value too great for base | G is not a hex digit |
echo "ibase=16; ff" | bc | 0 | bc reads lowercase letters as variables |
echo $((010)) | 8 | A 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 # FFFor the full reverse method, see the decimal to hex converter.
Frequently asked questions
How do you convert hex to decimal in Bash?
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?
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?
bc or Python for longer values.How do you convert a hex string stored in a variable?
echo $((16#$h)), where h holds the digits. Remove a 0x prefix first with ${h#0x}.Related tools
- Hex to Decimal Converter — convert any hex value with digit-by-digit steps
- Bulk Hex to Decimal Converter — convert a pasted list and download a CSV
- Little Endian Hex to Decimal — byte order for dumps and files
- Hex to Decimal in Python — int(), bytes and struct methods