The Google Sheets HEX2DEC function converts a signed hexadecimal number (base 16) into its decimal equivalent (base 10). Its syntax is HEX2DEC(signed_hexadecimal_number), where the argument is the hex value you want to convert, entered directly as text (e.g., "1A") or as a cell reference.
What HEX2DEC Does and Why It Matters
Hexadecimal is a base-16 numbering system that uses digits 0-9 and letters A-F. It is commonly used in computing for color codes (like #FF5733), memory addresses, and binary shorthand. The HEX2DEC function bridges the gap between this technical format and the decimal system most people use daily. Without it, you would need to manually multiply each digit by powers of 16, which is error-prone for large values. In Google Sheets, this function is part of the Engineering category, alongside other conversion tools like DEC2HEX, BIN2DEC, and OCT2DEC.
How the Function Works: Syntax and Arguments
The function takes exactly one argument: signed_hexadecimal_number. This is the hexadecimal value you want to convert. Google Sheets interprets it as a signed 10-bit number, meaning it can represent values from -512 to 511. The most significant bit (the leftmost bit) indicates the sign: if the highest bit is set, the number is negative. For example, HEX2DEC("1FF") returns 511, while HEX2DEC("200") returns -512. The argument can be a string in quotes, a cell reference containing the hex text, or a number that looks like hex (though it is safer to use text). If you use a number like 10, Sheets treats it as “10” (hex), not as decimal 10. The function is case-insensitive, so “1a” and “1A” both work.
Step-by-Step Examples with Real Formulas
Assume you have hex values in cells A2:A4. To convert them, place this formula in B2 and drag down:
- With A2 containing
"FF", enter=HEX2DEC(A2)in B2. The result is 255. - With A3 containing
"A", the formula returns 10. - With A4 containing
"FFFF", the formula returns -1 (because FFFF is the two’s complement representation of -1 in 16-bit signed arithmetic).
You can also use literal strings directly: =HEX2DEC("2F") returns 47. For a direct conversion of a binary value, you would use BIN2DEC, but HEX2DEC is for hex only.
Common Errors and How to Fix Them
The most frequent error is #NUM!, which appears when the hex value is too large for the signed 10-bit range (more than 10 bits). For example, HEX2DEC("400") exceeds 511 and returns #NUM!. Another error is #VALUE!, which occurs if the argument is not a valid hex string—for instance, if it contains characters outside 0-9 and A-F, like “G1”. To avoid these, ensure your hex values are within the range and formatted as text. If you have a hex number with a leading minus sign (e.g., “-1A”), Sheets does not accept it; instead, use the two’s complement representation (e.g., “FFE6” for -26). Also, if you reference an empty cell, the function returns 0.
Comparing HEX2DEC with Similar Conversion Functions
Google Sheets offers a full family of base conversion functions. DEC2HEX converts decimal to hex, BIN2DEC converts binary to decimal, and OCT2DEC converts octal to decimal. The key difference is the input base. HEX2DEC is specifically for hex, and it handles signed numbers differently than DEC2HEX does. For example, DEC2HEX(255) returns “FF”, but HEX2DEC("FF") returns 255. However, for negative numbers, DEC2HEX uses a two’s complement representation with a specified number of places (e.g., DEC2HEX(-1, 4) returns “FFFF”), while HEX2DEC reads that as -1. If you need to convert a large hex number beyond 10 bits, consider splitting it or using a custom script, as HEX2DEC is limited.
Practical Use Cases and Limitations
HEX2DEC is useful when importing data from systems that output hex, such as color pickers, network logs, or microcontroller readings. For example, if you have a list of CSS color codes in column A (like “1E90FF”), you can extract the red, green, and blue components by converting each pair: =HEX2DEC(LEFT(A2,2)), =HEX2DEC(MID(A2,3,2)), and =HEX2DEC(RIGHT(A2,2)). This yields 30, 144, and 255 for dodger blue. However, the 10-bit signed limit means you cannot convert a full 32-bit color directly; you must break it into bytes. Also, the function does not handle hex numbers with a decimal point or fractions—it only works with integers.
Frequently Asked Questions
Why does HEX2DEC return a negative number for values like “FFFF”?
Because Sheets treats the input as a signed number. The leftmost bit indicates the sign, and “FFFF” is the two’s complement representation of -1 in 16-bit. If you want the unsigned decimal equivalent (65535), you need to use a workaround, such as =HEX2DEC("FFFF")+65536 for 16-bit values, but this only works if you know the bit length.
Can I convert a hex number with more than 10 bits?
No, directly it will return #NUM!. The function is limited to signed 10-bit numbers. For larger values, split the hex into chunks or use a custom Google Apps Script function that handles arbitrary precision.
Is HEX2DEC case-sensitive?
No, it accepts both uppercase and lowercase letters. “1a” and “1A” produce the same result.
What if my hex number is stored as a number, not text?
It still works, but be careful: if you enter =HEX2DEC(10), Sheets interprets 10 as the hex string “10”, which equals 16 in decimal, not 10. To avoid confusion, always enclose hex values in quotes or use cell references with text formatting.
Does HEX2DEC work in Excel?
Yes, Excel has the same function with identical syntax and behavior. However, Excel’s version also supports a second optional argument for places, but Google Sheets does not. In Excel, HEX2DEC("FF") returns 255, same as Sheets.
How do I convert a decimal number to hex in Google Sheets?
Use the DEC2HEX function. For example, =DEC2HEX(255) returns “FF”. To pad with leading zeros, use =DEC2HEX(255, 4) which returns “00FF”.
Alternatives and Workarounds for Larger Numbers
If you need to convert hex values larger than 10 bits, you can use a custom formula with the HEX2DEC function in combination with arithmetic. For a 32-bit hex number in A1, you could break it into two 16-bit halves: =HEX2DEC(LEFT(A1,4))*65536 + HEX2DEC(RIGHT(A1,4)). This works because each 16-bit half is within the 10-bit limit? Actually, 16-bit values like “FFFF” are within the 10-bit signed range? No, they are not—”FFFF” is 16 bits and exceeds 10 bits, so that formula would fail. A better approach is to use a Google Apps Script custom function that parses the hex string manually using JavaScript’s parseInt with radix 16, which handles arbitrary precision up to 53 bits. To do this, go to Extensions > Apps Script, write a function that converts a string, and then call it from your sheet. Remember that such scripts are not built-in and require permission.
Related
- Google Sheets Split on Character: How-To & Examples
- Google Finance Mutual Fund Attributes Explained
- Budget and Balance Sheet: Key Differences
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Clear Excel Shortcut: Contents, Formats, and CellsTo clear the contents of selected Excel cells on Windows, select them and press Delete. This removes values…
- Excel Drop-Down Lists with Named Ranges: Setup and TipsTo create an Excel drop-down list from a named range, define a name for the cells containing your…
- Hide Column Headers in Power BI: Table & MatrixTo hide column headings in a Power BI table, select the visual, open Format visual > Visual >…
- Excel Freeze Panes: Keep Rows and Columns VisibleTo freeze panes in Excel for Microsoft 365 on Windows, select the cell immediately below the rows and…
- Google Sheets Percent Increase: Formula & ExamplesTo calculate a percent increase in Google Sheets, subtract the original value from the new value, divide that…
- Excel Consolidate: How to Combine Data Across SheetsExcel has no worksheet function named CONSOLIDATE. To combine ranges, use Data > Data Tools > Consolidate; if…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.