Hex to Decimal in AWK
Hex to Decimal in AWK. Enter your value into Hex Input and the Hex to Decimal in AWK tool instantly returns the Decimal Output plus the matching AWK one-liner. The fastest way to verify a bitwise AND between two hex operands is the hex bitwise calculator, no manual bit-by-bit work required.
echo "ff" | awk '{ printf "%d\n", strtonum("0x" $1) }'
# 255Staring at a column of values like 1F90 and need plain numbers? This guide shows you hex to decimal in awk with a few lines of scripting, so your hexadecimal values become decimal numbers right inside the pipeline. You get the short gawk one-liner, a portable fallback for Unix-based environments, and the checks that keep your log files and network data accurate.
How to Convert Hex to Decimal in AWK with strtonum and printf
The quickest route is a single built-in function. GNU AWK ships strtonum(), which reads a string such as 0x7B2C and returns a number, and you hand that number to printf or print. The pipeline below reads one hex string per line and prints its decimal form. Use the base36 to hex converter when a short-URL ID or generated token encoded in Base36 needs to be converted back to hex.
echo "7B2C" | awk '{ printf "%d\n", strtonum("0x" $1) }'
# Output: 31532Here "0x" $1 glues the prefix onto the field, strtonum parses the result as base-16, and %d prints the integer. You can drop printf and use plain print strtonum("0x" $1) when you do not need padding or a custom layout.
Method 1: Use gawk strtonum with a format specifier
Whenever a script has to keep columns aligned, the format specifier does the work. %d gives a bare integer, %8d right-aligns it in eight characters, and %08d pads with zeros. Running gawk explicitly makes it obvious which interpreter your script depends on:
printf 'web01 1F90\ndb02 0CEA\n' | gawk '{ printf "%-6s %6d\n", $1, strtonum("0x" $2) }'
# web01 8080
# db02 3306Each hex port in the second column becomes a decimal port while the first column passes through untouched.
Method 2: Manual conversion with a hex2dec function for older AWK versions
Some systems ship an older AWK that has no strtonum; the stock FreeBSD awk and mawk are common examples, and calling the missing function ends with an "undefined function" error. For those cases a manual conversion built on substr, index and toupper is fully portable:
function hex2dec(h, i, c, d, n) {
h = toupper(h)
sub(/^0X/, "", h)
n = 0
for (i = 1; i <= length(h); i++) {
c = substr(h, i, 1)
d = index("0123456789ABCDEF", c) - 1
if (d < 0) return -1
n = n * 16 + d
}
return n
}
{ print $1, hex2dec($2) }The loop walks the hex digits from left to right. For every character, index() finds its position in the digit list, which is its value, and the running total is multiplied by 16 before the next digit is added. A return value of -1 flags a character that is not valid.
Hexadecimal to Decimal Conversion: How Base-16 Arithmetic Works
Understanding the math lets you trust the script, and it makes every hexadecimal to decimal conversion easy to verify by hand. Hexadecimal is a base-16 number system with sixteen symbols, 0-9 and A-F, and each position carries a weight that is a power of 16. The decimal value of a number with digits \(d_{n-1} \dots d_1 d_0\) is:
$$\text{decimal} = \sum_{i=0}^{n-1} d_i \times 16^{i}$$
Place values and powers of 16
The rightmost digit has a place value of \(16^0 = 1\), the next one \(16^1 = 16\), then \(16^2 = 256\), then \(16^3 = 4096\). Take 7B2C, where B is 11 and C is 12:
$$7 \times 4096 + 11 \times 256 + 2 \times 16 + 12 \times 1 = 28672 + 2816 + 32 + 12 = 31532$$
That is exactly the number the strtonum pipeline printed above. The same base-16 arithmetic runs inside the manual function, one multiply-and-add per digit, which is why both methods agree.
Hex digit reference table
| Hex digit | Decimal value | Binary (4 bits) |
|---|---|---|
| 0-9 | 0-9 | 0000-1001 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
Because each hex digit stands for exactly four bits, two digits make one byte, which is why hexadecimal is the favourite notation for memory addresses, machine code and other binary data.
Hex to Decimal Conversion in AWK for Log Files and Hex IDs
Real input is rarely a clean column. Hex values hide inside system logs, memory dumps and binary protocols dumps, mixed with text, and inline data processing lets you convert them without any external helper.
Parsing hex fields from log files
Suppose a web server writes lines such as 10.20.30.40 GET /status 200 size=0x3F7A91 req=0x9E4. A short loop finds every hex field, splits off its name, and prints the decimal result:
awk '{ for (i = 1; i <= NF; i++)
if ($i ~ /^(size|req)=0x[0-9A-Fa-f]+$/) {
split($i, kv, "=")
printf "%s=%d\n", kv[1], strtonum(kv[2])
} }' access.log
# size=4160145
# req=2532The same pattern works for hex IDs and a hex timestamp field, and it keeps the original text intact when you print the other columns next to the converted ones.
Hex strings with or without the 0x prefix
The 0x prefix matters. strtonum("0x9E4") parses base-16, while strtonum("9E4") reads the string as a decimal number and stops at the first letter. That is why raw hex input always needs the prefix glued on first. Uppercase and lowercase letters both work, so 0x9e4 and 0x9E4 give the same answer; the manual function above handles case itself with toupper.
Checking a Router Heap Reading with Hex to Decimal Conversion in AWK
A network engineer is chasing random reboots on an edge router whose syslog reports memory as hexadecimal byte counts. The firmware gives its heap a hard limit of 32 MiB, which is 33,554,432 bytes, and the on-call runbook says to act at 90 percent. Two samples are in the file: heap_used=0x1C9F3A0 at 02:10 and heap_used=0x1CD7F10 at 02:25. Nothing in the log says whether either one is dangerous, so the engineer pastes both into a gawk command that does the hex to decimal conversion in awk and divides by the limit:
awk -F= '/heap_used/ { b = strtonum($2); printf "%d bytes, %.2f MiB, %.2f%%\n", b, b / 1048576, b / 33554432 * 100 }' router.logThe first line comes back as 30,012,320 bytes, 28.62 MiB, 89.44%. That sits just under the 90 percent line, so nothing needs to happen yet. The second sample prints 30,244,624 bytes, 28.84 MiB, 90.14%, which crosses the runbook's threshold of 30,198,988.8 bytes by 45,635 bytes. Fifteen minutes of growth equals 232,304 bytes, about 15.5 KB per minute.
At that rate the heap reaches the full 33,554,432 bytes in roughly 3,309,808 bytes divided by 15,487 bytes per minute, a little over 213 minutes, so the 02:25 reading predicts a failure near 06:00. The decision is concrete: the engineer schedules a controlled restart for the 04:00 maintenance window and reruns the same command on the next three samples to confirm the slope. Without the decimal numbers, the two hex strings differ only in the last four digits and look almost identical, which is exactly why the alert was missed the first time.
Common Hexadecimal Conversion Pitfalls in AWK Scripts
A conversion that runs is not always a conversion that is right. Three traps cause almost every bad result you will meet when you convert hex values in a pipeline.
The concatenation trap: 0x$7 is not "0x" $7
In awk the unquoted 0x is a number followed by a variable named x, and the missing operator means concatenation of two empty-or-zero values with the field. The unset variable x is auto set to 0 the first time awk sees it, so the text left behind starts with digits, and strtonum keeps only the leading digits and silently ignores everything from the first letter on. A field holding 08BA collapses to 8, even though the hexadecimal value really equals 2234, so the printed numbers look plausible but never match a hand check. Quote the prefix as a string, as in "0x" $7, and you get the full value.
Leading zeros and edge cases
Leading zeros are harmless for the number but matter for display. 00FF converts to 255 in both methods, and if the width must survive you need a %05d style format. Test edge cases before you trust a script, and know what each method does with them: strtonum("0x") on an empty field returns 0, strtonum("0xG1") stops at the G and also returns 0, while the manual hex2dec returns -1 for the same bad character. Very large values are stored as floating-point numbers, so anything past 253 loses precision in awk, and a 16-digit hex field needs a different tool.
Version and portability checks
Run awk --version first. If it reports GNU Awk, strtonum exists; if not, either install gawk or ship the hex2dec function with your script. Installing a second interpreter beside the system one avoids breaking older scripts that rely on the stock awk.
Hex to Decimal Conversion in Shell Scripting for Debugging and Networking
Hexadecimal turns up wherever developers inspect raw data, which makes a dependable hex to decimal conversion step a useful part of your shell scripting toolbox. While debugging, a crash report may give an offset as a hexadecimal number while your monitoring dashboard counts in decimal, and converting once inside the pipeline saves you a manual lookup. In networking work, ports, flags and addresses are printed in hexadecimal, so a quick pass lets you compare them with the decimal numbers in a firewall rule or an access list.
Take a hex-encoded IPv4 value such as 0A141E28. It is four bytes written as eight hex digits, and awk can split it into two-character pairs and convert each pair on its own:
echo "0A141E28" | awk '{ for (i = 1; i <= 7; i += 2)
printf "%d%s", strtonum("0x" substr($1, i, 2)), (i < 7 ? "." : "\n") }'
# 10.20.30.40The result matches the dotted form you would see in a log line, and the same trick rebuilds IP addresses from packet captures. Following a step-by-step routine keeps this reliable: extract the field, add the prefix, convert, then compare the output against one value you worked out by hand.
Why scripts beat copy and paste for programming tasks
Converting values one at a time in a browser works for a single number, but programming tasks usually involve thousands of lines. Awk's built-in functions and string handling make the whole file a single command, and the result is repeatable: the same input always produces the same output, so you can rerun it after every change. That repeatability is what developers value when a conversion feeds a report, a test fixture or a monitoring check.
Awk also supports other base conversions with the same pattern. Octal works with a different prefix and radix, and a binary string can be converted by multiplying by 2 instead of 16. The loop in the manual function stays almost unchanged; only the digit list and the multiplier differ, and the binary case is the easiest way to check your understanding of positional notation.
Hex to Decimal Converter Alternatives: bc, Perl, Python and More
Awk is not the only way to convert hex to decimal, and the right pick depends on how much of your pipeline is already built.
| Method | Example | Needs | Best for |
|---|---|---|---|
| gawk strtonum | strtonum("0x" $1) | GNU AWK | One-liner inside an existing awk script |
| Manual hex2dec function | hex2dec($1) | Any awk | Older AWK versions, portability |
| bc | echo "ibase=16; 7B2C" | bc | bc | Quick shell check, uppercase digits only |
| Perl | perl -ne 'print hex($_)' | Perl | Whole-line one-liner with split fields |
| printf in bash | printf "%d\n" 0x7B2C | Bash | A single known value |
Python, JavaScript and C++ equivalents
When the conversion moves out of the shell and into a program, every language has a built-in function. Python uses int("7B2C", 16), JavaScript uses parseInt("7B2C", 16), and C++ uses std::stoi("7B2C", nullptr, 16); all three return 31532. For one-off checks, a hex to decimal converter in your browser gives the same answer, and for the opposite direction look at decimal to hex tools or printf "%X".
Where Hexadecimal to Decimal Conversion in AWK Pays Off
Once the awk conversion is a reflex, you will use it for more than ports. Hex color codes split into red, green and blue pairs, so #2E8B57 reads as RGB 46, 139 and 87, which is the decimal triple many programming APIs expect. Awk gets there with strtonum("0x" substr($1, 2, 2)) for each pair. An IPv6 address is eight hex groups, and the group fe80 is 65152. Network data from packet captures, CLI tools output and sensor logs often print counters in hex, and the bitwise operations on them are far easier to reason about once you can see both forms side by side. Be careful with little endian byte order and two's complement negatives, since a plain conversion treats the bytes as one unsigned number.
A good habit is to build a small conversion table of values you expect, add basic error checking for characters that are not valid hexadecimal characters, and keep readability high by naming the function hex2dec and commenting the loop. Accuracy and efficiency both follow from that, and a tidy script is easy for the next developer or system administrator to debug.