Hex to Decimal in AWK
Convert a hex value to decimal, with the exact gawk strtonum() syntax — plain AWK doesn't parse 0x-prefixed strings as numbers automatically.

Built by
Swapnil SanghviFull-Stack Web Developer & WordPress Developer
Swapnil Sanghvi is a full-stack web and WordPress developer, UI designer, and full-time freelancer who builds and maintains Hex Calculator.
Further reading: hex — Stack Overflow
Why AWK Needs Special Handling for Hex
AWK's core design treats input as text, and its automatic string-to-number conversion only recognizes plain decimal notation. A string like "0xFF" used in arithmetic evaluates to 0 in standard awk, since AWK stops parsing at the non-decimal "x" character. gawk's strtonum() function exists specifically to bridge this gap.
gawk strtonum() Syntax Reference
| Hex value | gawk command | Result |
|---|---|---|
| FF | awk 'BEGIN { print strtonum("0xFF") }' | 255 |
| 1234ABCD | awk 'BEGIN { print strtonum("0x1234ABCD") }' | 305441741 |
| From a variable | awk -v hex="0xFF" 'BEGIN { print strtonum(hex) }' | 255 |
Where Hex to Decimal in AWK Actually Comes Up
Parsing Hex Values Out of a Log File
Log files that record error codes or addresses in hex often need converting to decimal within an awk script for further arithmetic or comparison.
awk '{ print strtonum($3) }' logfile
Processing Hex Fields in a Shell Pipeline
Using awk as a lightweight text-processing step in a shell pipeline that needs to convert hex-formatted fields to decimal for sorting or summing.
hex column -> decimal for sort -n
Debugging Why a Hex Comparison Silently Fails
Recognizing that an awk script comparing hex strings numerically is silently failing because strtonum() wasn't used — a common gotcha for anyone new to awk's string/number handling.
"0xFF" + 0 evaluates to 0, not 255, without strtonum()
Why Use This Calculator Instead of Doing It by Hand
- Gives the exact decimal value strtonum() would return, for a quick sanity check
- Runs entirely in your browser — nothing you type gets sent anywhere
- Saves testing a strtonum() call interactively just to check one value
- Reference table shows the exact gawk command syntax alongside the result
Limitations
- This is gawk-specific guidance — other awk implementations (mawk, the original one-true-awk) generally lack strtonum() entirely.
- This calculator computes the resulting decimal value — it doesn't execute or validate actual awk syntax.
Frequently Asked Questions
Does AWK automatically convert a hex string to a number?
No — standard POSIX awk treats a string like "0xFF" as text in a numeric context, not as the number 255. You need gawk's strtonum() function to parse it as hex explicitly.
How do I convert hex to decimal in gawk specifically?
Use strtonum("0xFF") — gawk's strtonum() recognizes the 0x prefix and correctly parses it as a hex number, returning 255.
What about plain (non-gawk) awk implementations, like mawk or the original awk?
They generally don't have strtonum() at all — you'd need to implement the hex-to-decimal conversion manually with a loop over the characters, or preprocess the value with another tool before passing it to awk.
Is there an older, deprecated way to do this in gawk?
Older gawk versions supported a --non-decimal-data command-line option that made gawk auto-recognize 0x-prefixed strings as hex in numeric context, but this is deprecated and strtonum() is the recommended approach now.