Convert hex to decimal in C
strtoul and strtoull
Call strtoul() from <stdlib.h> with the hex string, an end pointer and base 16. strtoul returns an unsigned long, and strtoull returns an unsigned long long for 64-bit values.
#include <stdlib.h>
char *end;
unsigned long v = strtoul("1A3F", &end, 16); // 6719The end pointer stores the address of the first character that strtoul did not consume. When *end is '\0', the entire string was valid hex. When it points to a non-null character, the string contained something unexpected.
strtoul("FFzz", &end, 16) // 255, *end points to 'z'
strtoul("zz", &end, 16) // 0, *end points to 'z' — no digits consumedPass base 0 instead of 16 to let strtoul detect the base from the prefix: 0x for hex, 0 for octal, no prefix for decimal. Type a string in the picture to see where the end pointer lands.
sscanf
sscanf with the %x format specifier reads a hex string into an unsigned integer. Use %lx for unsigned long and %llx for unsigned long long.
unsigned int v;
sscanf("1A3F", "%x", &v); // v = 6719
sscanf("0x1A3F", "%x", &v); // v = 6719 — the prefix is acceptedsscanf returns the number of fields it filled. A return value of 0 or EOF means the string had no valid hex digits.
Error detection in C
C does not throw exceptions. Check the end pointer and errno after every strtoul call.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
char *end;
errno = 0;
unsigned long long v = strtoull(input, &end, 16);
if (end == input)
puts("No hex digits found");
else if (*end != '\0')
printf("Stopped at '%c'\n", *end);
else if (errno == ERANGE)
puts("Value too large for unsigned long long");| Check | Meaning |
|---|---|
end == input | No digits consumed, so the string is not hex |
*end != '\0' | Partial parse, trailing characters remain |
errno == ERANGE | Overflow, the value exceeds the type's range |
Set errno to 0 before the call, because strtoul does not clear it on success.
Signed hex in C
Cast the unsigned result to a signed type, and C reads the bits as two's complement.
(signed char) strtoul("FF", NULL, 16) // -1 (8-bit)
(short) strtoul("FF9C", NULL, 16) // -100 (16-bit)
(int) strtoul("FFFFFFFF", NULL, 16) // -1 (32-bit)
(long long) strtoull("FFFFFFFFFFFFFFFF", NULL, 16) // -1 (64-bit)The cast keeps only the bits that fit the target type. strtoul("FF", NULL, 16) returns 255 as an unsigned long, but casting to signed char gives −1 because the 8-bit pattern 11111111 is −1 in two's complement. The C standard leaves this conversion implementation-defined before C23, and every common compiler gives the two's complement result.
For a visual breakdown of sign bits at every width, see the signed hex to decimal converter.
printf format specifiers
Print the same value as hex or decimal by choosing the format:
unsigned int v = 0xFF;
printf("%u\n", v); // 255 (unsigned decimal)
printf("%d\n", v); // 255 (signed decimal, positive here)
printf("%x\n", v); // ff (lowercase hex)
printf("%X\n", v); // FF (uppercase hex)
printf("%08X\n", v); // 000000FF (zero-padded to 8 digits)Convert hex to decimal in C++
std::stoi, std::stoul, std::stoull
C++ wraps the C functions and adds exception handling. std::stoi(s, nullptr, 16) converts to int, std::stoul to unsigned long, and std::stoull to unsigned long long.
#include <string>
int v = std::stoi("1A3F", nullptr, 16); // 6719
unsigned long long w = std::stoull("FFFFFFFF", nullptr, 16); // 4294967295std::stoi throws std::invalid_argument when the string has no valid digits, and std::out_of_range when the value overflows. Because these functions wrap strtol, they accept a 0x prefix with base 16 and stop quietly at the first bad character unless you pass a position pointer.
std::from_chars (C++17)
std::from_chars does not allocate, does not throw, and reports errors through a return struct.
#include <charconv>
#include <iostream>
#include <string>
std::string s = "1A3F";
int v;
auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), v, 16);
if (ec == std::errc{})
std::cout << v << "\n"; // 6719std::from_chars rejects the 0x prefix. It is the fastest standard option for parsing hex in C++ because it avoids locale handling and memory allocation.
std::hex with streams
#include <sstream>
unsigned int v;
std::istringstream("FF") >> std::hex >> v; // 255Stream extraction with std::hex also accepts a 0x prefix on input. Without the manipulator, the stream reads decimal.
Convert hex to decimal in C#
Convert.ToInt32
Convert.ToInt32(s, 16) converts a hex string to a signed 32-bit integer. It accepts the 0x and 0X prefix and reads 8-digit values as two's complement, so FFFFFFFF returns −1.
Convert.ToInt32("FF", 16) // 255
Convert.ToInt32("FFFFFFFF", 16) // -1
Convert.ToInt32("0xFF", 16) // 255Convert.ToInt64(s, 16) returns a long for values above 2³¹ − 1.
int.Parse with NumberStyles
int.Parse(s, NumberStyles.HexNumber) does not accept a 0x prefix. Strip the prefix first, or use Convert.ToInt32, which does. It also reads FFFFFFFF as −1 in an int.
using System.Globalization;
int v = int.Parse("FF", NumberStyles.HexNumber); // 255
uint u = uint.Parse("FFFFFFFF", NumberStyles.HexNumber); // 4294967295For values beyond 64 bits, use System.Numerics.BigInteger.Parse(s, NumberStyles.HexNumber).
Decimal to hex
C
printf("%x", 255); // ff
printf("%X", 255); // FF
printf("0x%X", 255); // 0xFFC++
std::cout << std::hex << 255; // ff
std::cout << std::hex << std::uppercase << 255; // FFOr use std::format (C++20):
std::format("{:X}", 255) // "FF"C#
(255).ToString("x") // "ff"
(255).ToString("X4") // "00FF"
$"0x{255:X}" // "0xFF"For the full reverse conversion and worked examples, see the decimal to hex converter.
Method comparison
| Method | Language | Max value | Signed | Accepts 0x | Error signal |
|---|---|---|---|---|---|
strtoul(s, &end, 16) | C | FFFFFFFF (32-bit) or FFFFFFFFFFFFFFFF (64-bit) | No | Yes | endptr + errno |
sscanf(s, "%x", &v) | C | Depends on format | No | Yes | Return value |
std::stoul(s, nullptr, 16) | C++ | FFFFFFFF or FFFFFFFFFFFFFFFF | No | Yes | Exceptions |
std::from_chars | C++17 | Type-dependent | Type-dependent | No | Error code |
Convert.ToInt32(s, 16) | C# | FFFFFFFF | Yes (two's complement) | Yes | Exceptions |
int.Parse(s, HexNumber) | C# | FFFFFFFF | Yes (two's complement) | No | Exceptions |
In C, start with strtoul(s, &end, 16) and check endptr and errno. In C++, use std::from_chars for speed or std::stoull for convenience. In C#, use Convert.ToInt32(s, 16) for most conversions.
Try every method on one string
Type one string and see how every method in the table reacts.
Frequently asked questions
How do you convert hex to decimal in C?
strtoul("FF", NULL, 16) to convert the hex string "FF" to the unsigned long 255. The third argument, 16, sets the base. Use strtoull for values that exceed 32 bits.Does strtoul accept the 0x prefix?
strtoul("0xFF", NULL, 16) returns 255. With base 0, strtoul reads the 0x prefix and picks hex automatically.How do you detect errors after strtoul?
errno. When endptr equals the input pointer, no digits were consumed. When *endptr is not '\0', trailing characters remain. When errno equals ERANGE, the value overflowed.How do you convert signed hex in C?
strtoul result to a signed type: (int) strtoul("FFFFFFFF", NULL, 16) returns −1. The cast keeps the lower bits and reads them as two's complement at the target width. The C standard calls an out-of-range cast implementation-defined before C23, but every common compiler uses two's complement.What is the difference between strtoul and sscanf for hex?
strtoul returns the parsed value and reports where parsing stopped through its end pointer. sscanf reads hex with the %x format specifier and returns the number of fields filled. strtoul gives finer error control; sscanf is shorter when the input format is known.How do you convert hex to decimal in C#?
Convert.ToInt32("FF", 16) to get 255. This method accepts the 0x prefix and reads 8-digit hex values as signed two's complement. For unsigned results, use Convert.ToUInt32(s, 16).Does C++ std::from_chars accept the 0x prefix?
std::from_chars rejects the prefix and stops at the x. For "0xFF" it reads the leading 0, reports success and leaves the pointer at the x, so check the returned pointer as well as the error code. Strip the prefix before calling it. std::stoi and std::stoul wrap strtol and strtoul, so they do accept a 0x prefix with base 16.Related tools
- Hex to decimal converter — convert hex to decimal with digit-by-digit steps
- Signed hex to decimal — interpret hex as a signed two's complement integer
- Hex to decimal in Java — Integer.parseInt, Long.parseLong and BigInteger methods
- Hex to decimal in Python — int(), bytes and struct methods