Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to write a number in hexadecimal format
Converting numbers to hexadecimal format in C# can be accomplished using various format specifiers. Hexadecimal is a base-16 number system commonly used in programming for representing binary data in a more readable format.
Syntax
Following are the main hexadecimal format specifiers in C# −
{0:x} // lowercase hexadecimal
{0:X} // uppercase hexadecimal
{0:x8} // lowercase with 8 digits (zero-padded)
{0:X8} // uppercase with 8 digits (zero-padded)
You can also use the ToString() method with format specifiers −
number.ToString("x") // lowercase hexadecimal
number.ToString("X") // uppercase hexadecimal
number.ToString("x8") // lowercase with padding
Using Format Specifiers with Console.WriteLine
The most common approach is using format specifiers with Console.WriteLine −
using System;
class Demo {
static void Main() {
int a = 12250;
Console.WriteLine("Original number: " + a);
Console.WriteLine("Lowercase hex: {0:x}", a);
Console.WriteLine("Lowercase hex (8 digits): {0:x8}", a);
Console.WriteLine("Uppercase hex: {0:X}", a);
Console.WriteLine("Uppercase hex (8 digits): {0:X8}", a);
}
}
The output of the above code is −
Original number: 12250 Lowercase hex: 2fda Lowercase hex (8 digits): 00002fda Uppercase hex: 2FDA Uppercase hex (8 digits): 00002FDA
Using ToString() Method
Another approach is using the ToString() method with hexadecimal format strings −
using System;
class HexExample {
static void Main() {
int number = 255;
Console.WriteLine("Number: " + number);
Console.WriteLine("Hex (lowercase): " + number.ToString("x"));
Console.WriteLine("Hex (uppercase): " + number.ToString("X"));
Console.WriteLine("Hex (4 digits): " + number.ToString("x4"));
Console.WriteLine("Hex (uppercase, 4 digits): " + number.ToString("X4"));
}
}
The output of the above code is −
Number: 255 Hex (lowercase): ff Hex (uppercase): FF Hex (4 digits): 00ff Hex (uppercase, 4 digits): 00FF
Converting Different Data Types
You can convert various integer data types to hexadecimal format −
using System;
class MultiTypeHex {
static void Main() {
byte byteValue = 200;
short shortValue = 4096;
long longValue = 1000000;
Console.WriteLine("Byte to hex: " + byteValue.ToString("X2"));
Console.WriteLine("Short to hex: " + shortValue.ToString("X4"));
Console.WriteLine("Long to hex: " + longValue.ToString("X"));
Console.WriteLine("Using format specifiers:");
Console.WriteLine("Byte: {0:X2}", byteValue);
Console.WriteLine("Short: {0:X4}", shortValue);
Console.WriteLine("Long: {0:X}", longValue);
}
}
The output of the above code is −
Byte to hex: C8 Short to hex: 1000 Long to hex: F4240 Using format specifiers: Byte: C8 Short: 1000 Long: F4240
Format Specifier Comparison
| Format Specifier | Description | Example (12250) |
|---|---|---|
| x | Lowercase hexadecimal | 2fda |
| X | Uppercase hexadecimal | 2FDA |
| x8 | Lowercase, 8 digits (zero-padded) | 00002fda |
| X8 | Uppercase, 8 digits (zero-padded) | 00002FDA |
Conclusion
C# provides flexible options for converting numbers to hexadecimal format using format specifiers like {0:x}, {0:X} with Console.WriteLine, or the ToString() method with format strings. The digit padding option allows consistent formatting for display purposes.
