Hex Decimal in AWK

Live as you type. Everything runs locally — nothing is sent anywhere.

Batch conversion

Paste one value per line, or comma-separated. Free and unlimited — export straight to CSV.

InputOutput

Results will appear here.

AWK's numbers-are-strings design, and why hex needs special handling

AWK was designed as a text-processing language built around fields and records, and its type system reflects that: a value is treated as a number or a string depending on context, with automatic conversion between the two. That flexible design has one consequence relevant here — a bare string like"0xFF" isn't automatically interpreted as the number 255 in most AWK arithmetic contexts the way it would be in C, Python, or JavaScript. AWK generally needs to be told explicitly that a string should be parsed as a hexadecimal number, which is exactly what strtonum() exists to do.

strtonum() is a gawk extension, not POSIX AWK

strtonum() is provided by gawk (GNU AWK), the most widely installed AWK implementation on Linux systems, but it isn't part of the POSIX AWK specification and isn't guaranteed to exist in every AWK variant — mawk and the original one-true-awk, for instance, don't include it. Scripts intended to run under strict POSIX AWK or a minimal AWK implementation may need a manual hex-parsing routine instead (looping over each character and accumulating a value), rather than relying on strtonum()being available. Checking which AWK implementation a target system actually provides — often justawk --version or checking whether the binary is a symlink to gawk — is worth doing before depending on this function in a portable script.

The reverse direction needs no special function

Converting decimal to hex is more portable than the reverse: printf's %X (or%x for lowercase) format specifier is part of standard, POSIX-compliant AWK and works identically across gawk, mawk, and other implementations, since formatting a number as hexadecimal text for output doesn't require any special numeric-parsing logic — it's the same underlying operationprintf performs in C and many other languages. This asymmetry (hex-to-decimal needing a gawk-specific function, decimal-to-hex working everywhere) is worth keeping in mind when writing an AWK script that needs to run across different systems.

Where this comes up: quick command-line conversions

AWK one-liners like the ones this tool generates are commonly used directly from a shell prompt for a fast, no-setup conversion — checking what a hex value from a log line or a memory dump equals in decimal, or vice versa, without opening a calculator, a REPL, or writing a full script. That's the main practical niche this tool fills: generating a copy-pasteable, correct one-liner for a task that's common enough to want quickly, but easy to get slightly wrong from memory (forgetting the 0xprefix inside the strtonum() argument, or mixing up %x and %d, are both common mistakes when typing these by hand).

A worked example

Converting hex FF to decimal: awk 'BEGIN { print strtonum("0xFF") }'prints 255. The 0x prefix inside the quoted string is what tells strtonum() to parse the value as hexadecimal rather than decimal or octal — without it, strtonum("FF")would fail to parse as a number at all, since bare "FF" isn't a valid decimal numeral. Going the other direction, converting decimal 255 to hex: awk 'BEGIN { printf "%X\n", 255 }' printsFF, using the uppercase %X specifier — lowercase %x would printff instead, with the case choice affecting only formatting, not the underlying value.

AWK in a broader shell-scripting context

AWK is frequently reached for specifically because it's preinstalled on nearly every Unix-like system, making it a dependency-free choice for text processing in shell scripts and one-liners where installing Python or another interpreter isn't practical or desired. Hex/decimal conversion inside a larger AWK script — for instance, parsing a log format that mixes hex addresses with decimal fields — benefits from the same strtonum()/printf pair covered here, just embedded inside a larger pattern-action program rather than run as a standalone one-liner.

FAQ

Why does AWK need strtonum() for hex?
AWK doesn’t parse 0x-prefixed strings as numbers automatically in arithmetic context — strtonum() (a gawk extension) explicitly interprets a 0x-prefixed string as hex.
Does this work in POSIX awk, or only gawk?
strtonum() is a gawk extension, not part of POSIX awk. The decimal-to-hex direction (printf "%X") works in any standard awk.
Can I generate snippets for a list of values at once?
Yes — use the batch converter below. Paste one value per line or comma-separated, and export the results as a CSV file.
g then:h homeb basee encodingc colorp programming