Convert FFFFFF to decimal value (C language)
Categories:
Converting Hexadecimal Color Codes to Decimal in C

Learn how to convert a hexadecimal color code like 'FFFFFF' into its corresponding decimal integer value in the C programming language, covering various methods and considerations.
Hexadecimal color codes are a common way to represent colors in web development and graphics programming. A hex code like FFFFFF
represents pure white, where each pair of hexadecimal digits (FF) corresponds to the red, green, and blue components, respectively. In C programming, you might need to convert such a string representation into a single decimal integer for various operations, such as storing it efficiently or performing bitwise manipulations. This article will guide you through different approaches to achieve this conversion.
Understanding Hexadecimal to Decimal Conversion
Before diving into the C code, it's crucial to understand the mathematical principle behind hexadecimal to decimal conversion. Hexadecimal (base-16) uses digits 0-9 and letters A-F (or a-f) to represent values. Each position in a hexadecimal number represents a power of 16. For example, FFFFFF
can be broken down as follows:
F
at position 0 (rightmost) = F * 16^0 = 15 * 1 = 15
F
at position 1 = F * 16^1 = 15 * 16 = 240
F
at position 2 = F * 16^2 = 15 * 256 = 3840
...and so on.
For a 6-digit hex code like RRGGBB
, the decimal equivalent is (RR * 16^4) + (GG * 16^2) + (BB * 16^0)
. More generally, for a hex string, you can iterate through its characters, convert each hex digit to its decimal equivalent, and accumulate the result by multiplying by 16 for each position.
flowchart TD A[Start] --> B{Input Hex String (e.g., "FFFFFF")} B --> C{Initialize Decimal Value = 0} C --> D{Iterate through each Hex Digit} D --> E{Convert Hex Digit to Decimal (0-15)} E --> F{Decimal Value = (Decimal Value * 16) + Digit Value} F --> G{More Digits?} G -- Yes --> D G -- No --> H[Output Decimal Value] H --> I[End]
Flowchart illustrating the general hexadecimal to decimal conversion process.
Method 1: Using strtol
for Direct Conversion
The C standard library provides a convenient function, strtol
(string to long), which can directly convert a string representation of a number in a specified base to a long
integer. This is often the most straightforward and robust method for hexadecimal conversions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
const char* hex_string = "FFFFFF";
long decimal_value;
// Convert hex string to long decimal using base 16
decimal_value = strtol(hex_string, NULL, 16);
printf("Hexadecimal: %s\n", hex_string);
printf("Decimal: %ld\n", decimal_value);
// Example with a different hex value
hex_string = "000000";
decimal_value = strtol(hex_string, NULL, 16);
printf("Hexadecimal: %s\n", hex_string);
printf("Decimal: %ld\n", decimal_value);
hex_string = "1A2B3C";
decimal_value = strtol(hex_string, NULL, 16);
printf("Hexadecimal: %s\n", hex_string);
printf("Decimal: %ld\n", decimal_value);
return 0;
}
Using strtol
to convert hexadecimal strings to decimal integers.
strtol
(endptr
) can be used to detect conversion errors. If endptr
is not NULL
, it will point to the first character in hex_string
that was not part of the conversion. If *endptr
is not \0'
, it means the entire string was not converted.Method 2: Manual Conversion with Character Processing
For educational purposes or scenarios where strtol
might be restricted (e.g., embedded systems with minimal libraries), you can implement the conversion manually. This involves iterating through the hexadecimal string, converting each character to its decimal equivalent, and accumulating the result.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function to convert a single hex character to its decimal value
int hex_char_to_int(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
return -1; // Error for invalid hex character
}
int main() {
const char* hex_string = "FFFFFF";
unsigned int decimal_value = 0;
int len = strlen(hex_string);
for (int i = 0; i < len; i++) {
int digit_value = hex_char_to_int(hex_string[i]);
if (digit_value == -1) {
printf("Error: Invalid hexadecimal character '%c'\n", hex_string[i]);
return 1;
}
decimal_value = (decimal_value * 16) + digit_value;
}
printf("Hexadecimal: %s\n", hex_string);
printf("Decimal: %u\n", decimal_value);
// Example with a different hex value
hex_string = "1A2B3C";
decimal_value = 0;
len = strlen(hex_string);
for (int i = 0; i < len; i++) {
int digit_value = hex_char_to_int(hex_string[i]);
if (digit_value == -1) { /* handle error */ break; }
decimal_value = (decimal_value * 16) + digit_value;
}
printf("Hexadecimal: %s\n", hex_string);
printf("Decimal: %u\n", decimal_value);
return 0;
}
Manual conversion of a hexadecimal string to a decimal integer.
hex_char_to_int
function in the example returns -1
for invalid input, which should be checked by the calling code.Considerations for Data Types and Range
When converting hexadecimal values, especially those representing colors, it's important to choose the correct data type to store the result. A 6-digit hexadecimal number like FFFFFF
(which is 16^6 - 1 = 16,777,215
in decimal) fits comfortably within a 32-bit unsigned integer (unsigned int
or uint32_t
). If you were to convert longer hexadecimal strings, you might need long long
or unsigned long long
to prevent overflow.
1. Include necessary headers
For strtol
, include <stdlib.h>
. For manual conversion, <string.h>
for strlen
and <ctype.h>
for toupper
(if you want to normalize case) are useful.
2. Choose your conversion method
For most applications, strtol
is preferred due to its simplicity and robustness. Manual conversion offers more control and is useful for learning or constrained environments.
3. Handle potential errors
Always consider what happens if the input string is not a valid hexadecimal number. strtol
can indicate errors via its endptr
argument, while manual methods require explicit checks.
4. Select appropriate data type
Ensure the target integer type (int
, long
, unsigned int
, etc.) is large enough to hold the converted decimal value to prevent overflow.