Int to Hex in C#
Int to Hex in C#. Type your number into Int Input and the Int to Hex in C# tool instantly returns the Hex Output plus the matching C# snippet. The fastest way to check what color a UIColor float value actually renders is to run it through the uicolor to hex converter.
int value = 255;
string hex = value.ToString("X");
// hex == "FF"Need to turn a whole number into a base-16 string inside your program? This int to hex in C# guide shows you how to convert any integer to a hexadecimal value with one line of code, and the converter above checks your answer instantly. By the end you will know which method fits your project, how padding and negative numbers behave, and how to cross-check the hex output by hand.
Int to Hex in C#: Three Built-In Ways to Convert Integers to Hexadecimal
The .NET base library gives you three everyday ways to convert an integer into a hexadecimal string. Every example below uses the same input, the decimal value 51966, so you can compare the results side by side. The correct answer is CAFE in uppercase or cafe in lowercase. Paste your Base32 string into the base32 to hex converter whenever you need the underlying hex bytes rather than the encoded text.
Method 1: Using ToString("X") for the quickest int to hex conversion
The simplest option is the ToString() method of the int type with the format specifier "X". It returns the hex number as a string with no prefix.
int number = 51966;
string hex = number.ToString("X");
Console.WriteLine(hex); // Output: CAFEAn uppercase "X" produces the letters A to F, and a lowercase "x" produces a to f, so number.ToString("x") prints cafe. This is the method to reach for in almost every case.
Method 2: Using Convert.ToString with base 16
Convert.ToString takes the target base as a second argument, so one call turns a decimal integer into binary, octal or hex. Pass 16 for hexadecimal and 2 for binary.
int value = 51966;
string hex = Convert.ToString(value, 16);
Console.WriteLine(hex); // Output: cafeNotice that this method always returns lowercase letters, which matters when you compare strings later. It is also handy when a base other than 16 is chosen at run time.
Method 3: Using BitConverter for byte arrays
When you work with memory blocks or network packets you often want the raw bytes rather than the number itself. BitConverter.GetBytes returns the four bytes of an int, and BitConverter.ToString turns them into hex pairs.
int value = 51966;
byte[] bytes = BitConverter.GetBytes(value);
string hex = BitConverter.ToString(bytes).Replace("-", "");
Console.WriteLine(hex); // Output (little endian): FECA0000The bytes come out reversed because most desktop CPUs are little endian, which makes this approach platform-dependent. Use it for byte-level work, and use Method 1 when you simply want the hex digit string of the number.
How the Integer to Hex Converter Works
Hexadecimal is a base 16 number system, so each position holds a value from 0 to 15, and the letters A to F stand for 10 through 15. Because 16 is a power of two, one hex digit maps to exactly four bits. Those four-bit groups are called nibbles, and they are why programmers prefer hex over long strings of binary digits. Use the octal to decimal converter to convert an octal value into plain base-10 output in a single click.
The positional formula behind every hex value
Each hex digit is multiplied by 16 raised to an exponent equal to its position, and the products are added together to give the decimal (base 10) value:
$$N = d_{n} \times 16^{n} + \dots + d_{1} \times 16^{1} + d_{0} \times 16^{0}$$
To go the other way, divide by 16 repeatedly and read the remainders from last to first. The remainder step is what the modulus operator % does in code, and each remainder is one hex digit.
\(N \bmod 16 = \text{remainder}\), then \(N = \lfloor N \div 16 \rfloor\) until \(N = 0\).
Decimal to hex: a worked example with 51966
Divide the decimal number 51966 by 16 until the quotient reaches zero. Each remainder becomes one digit, and you read the list from the bottom up.
| Step | Division | Quotient | Remainder | Hex digit |
|---|---|---|---|---|
| 1 | 51966 ÷ 16 | 3247 | 14 | E |
| 2 | 3247 ÷ 16 | 202 | 15 | F |
| 3 | 202 ÷ 16 | 12 | 10 | A |
| 4 | 12 ÷ 16 | 0 | 12 | C |
Reading the digits from the last step to the first gives CAFE. To verify it, expand the result: \(12 \times 16^{3} + 10 \times 16^{2} + 15 \times 16^{1} + 14 \times 16^{0} = 49152 + 2560 + 240 + 14 = 51966\). Written as binary, the same value is 1100 1010 1111 1110, one 4-bit group (a nibble) for each hex digit.
Decimal, binary and hex digits side by side
Memorising the sixteen digits makes decimal to hex work in your head. Each row pairs a decimal value with its binary nibble and its hex digit.
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0 |
| 5 | 0101 | 5 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 12 | 1100 | C |
| 14 | 1110 | E |
| 15 | 1111 | F |
Hex is also the natural way to write binary masks. A bitwise AND with 0xFF keeps the lowest byte of an integer, and in C# (value & 0xFF).ToString("X2") prints that byte as two hex digits. Letters are a separate matter: the ASCII code for the character "A" is 65 in decimal, or 41 in hex, which is not the same thing as the hex digit A (10 in decimal).
Int to Hex Format Specifier Options: Padding, Case and Negative Values
A bare ToString("X") gives you the shortest possible string. Real programs usually need a fixed width or a defined case when they print a decimal count or a binary flag, and the format specifier handles both.
Leading zeros with X2, X4 and X8
Add a number after the letter to set the minimum number of digits. The converter pads the left side with zeros, which is exactly what you want for byte values, addresses and fixed-width logs.
| Code | Input | Output |
|---|---|---|
26.ToString("X2") | 26 | 1A |
5.ToString("X2") | 5 | 05 |
51966.ToString("X8") | 51966 | 0000CAFE |
If the number needs more digits than you ask for, nothing is cut off. 51966.ToString("X2") still returns CAFE.
Negative integers and two's complement
You can convert negative integers too. A C# int uses 32-bit two's complement, so (-2).ToString("X") returns FFFFFFFE rather than a minus sign. At the other end of the range, int.MaxValue, 2147483647, becomes 7FFFFFFF. If you want a signed representation like -2 instead, convert the absolute value and add the sign yourself.
Uppercase hex versus lowercase
Use "X" for uppercase hex and "x" for lowercase. Both describe the same number, but string comparisons are case-sensitive, so settle on one style across a code base, or compare with StringComparison.OrdinalIgnoreCase.
Checking a CAN Frame ID with an Integer to Hexadecimal Converter
A firmware developer is reading a bus log where every frame ID is printed as a plain decimal int, and one line shows 1953. The vehicle spec lists identifiers in hex, so she runs the value through an integer to hexadecimal check before filing the bug.
She enters 1953 and sets the width to three digits. The calculation is short: 1953 ÷ 16 is 122 remainder 1, then 122 ÷ 16 is 7 remainder 10, and 7 ÷ 16 is 0 remainder 7. Read from the bottom up, the digits are 7, A and 1, so the converter returns 7A1. In her test harness the same value comes from 1953.ToString("X3"), and the two outputs match.
She then checks the hex value against the spec. A standard CAN frame carries an 11-bit identifier, so the highest valid value is 0x7FF (2047 in decimal), and 0x7A1 sits comfortably under it.
The neighbouring log line shows 2017, so she converts that too: 2017 divides into 126 remainder 1, then 7 remainder 14, and 2017.ToString("X3") returns 7E1. Because the spec lists its diagnostic request IDs as 0x7E0 to 0x7EF, the hex form lets her match that line against the spec at once, and she filters the capture on 0x7E1.
Integer to Hexadecimal for a Color Hex Code
Web colors are three bytes written as hex, so an RGB triple converts neatly with X2 padding. Each channel is a decimal value from 0 to 255, so a teal with red 26, green 188 and blue 156 works like this:
int r = 26, g = 188, b = 156;
string color = $"#{r:X2}{g:X2}{b:X2}";
Console.WriteLine(color); // Output: #1ABC9CEach channel needs two digits, so a channel value of 5 must print as 05, not 5, or the whole code shifts. That is the reason the padding specifier matters here.
Int to Hex in C Compared With C#
If you also write C programming code, the idea is the same but the syntax differs. C has no string method on integers, so you hand the number to the printf function with a format specifier instead, and the same printf call also prints decimal or octal.
int decimalNumber = 51966;
printf("Hexadecimal number is: %X", decimalNumber); // CAFEUse %X for uppercase and %x for lowercase. C also lets you write the conversion yourself with a loop that takes the remainder of a division by 16, as in the table above, but in C# the built-in function is the better choice.
Comparing Decimal to Hexadecimal Conversion Methods in C#
Pick the method by the result type you need, not by habit. The comparison below summarises the syntax and the trade-offs.
| Method | Syntax | Case of letters | Best for |
|---|---|---|---|
| ToString("X") | n.ToString("X") | Your choice (X or x) | Everyday int to hex conversion |
| Convert.ToString | Convert.ToString(n, 16) | Lowercase | Switching between base 2, 8 and 16 |
| BitConverter | BitConverter.ToString(bytes) | Uppercase | Byte arrays and little endian inspection |
| Manual loop | n % 16 and n / 16 | Your choice | Learning how base 16 works |
Why Convert an Int to Hex in Real Projects
A hex string is compact and far easier to read than binary, which is why it shows up in so many low-level jobs. Typical uses include:
- Debugging low-level code, where register and flag values are easier to spot in hex
- Printing memory locations and offsets in logs
- Networking and protocol design, where headers are described byte by byte
- Embedded systems and digital electronics, where hardware documents list values in hexadecimal
- Game development, where color and ID values are packed into one integer
In C#, each of these computer science cases comes down to a single ToString("X") or ToString("X2") call. If you want to check a result before you ship it, paste the input into the calculator above and compare its hex value with your program output. Whichever binary, decimal or hex form you start from, the conversion rules stay the same.