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# Hexadecimal ("X") Format Specifier
The hexadecimal ("X") format specifier is used to convert a number to a string of hexadecimal digits. The case of the format specifier determines whether hexadecimal digits greater than 9 are displayed in uppercase or lowercase.
Use "X" for uppercase hexadecimal letters (A, B, C, D, E, F) and "x" for lowercase hexadecimal letters (a, b, c, d, e, f).
Syntax
Following is the syntax for using the hexadecimal format specifier −
number.ToString("X") // uppercase
number.ToString("x") // lowercase
number.ToString("X4") // uppercase with minimum 4 digits
number.ToString("x8") // lowercase with minimum 8 digits
Basic Hexadecimal Formatting
Example
using System;
class Demo {
static void Main() {
int num = 345672832;
Console.WriteLine("Decimal: " + num);
Console.WriteLine("Uppercase hex: " + num.ToString("X"));
Console.WriteLine("Lowercase hex: " + num.ToString("x"));
num = 0x307e;
Console.WriteLine("\nDecimal: " + num);
Console.WriteLine("Uppercase hex: " + num.ToString("X"));
Console.WriteLine("Lowercase hex: " + num.ToString("x"));
}
}
The output of the above code is −
Decimal: 345672832 Uppercase hex: 149A8C80 Lowercase hex: 149a8c80 Decimal: 12414 Uppercase hex: 307E Lowercase hex: 307e
Using Precision Specifiers
You can specify the minimum number of digits by adding a precision specifier after the format specifier −
Example
using System;
class Demo {
static void Main() {
int num = 255;
Console.WriteLine("Default: " + num.ToString("X"));
Console.WriteLine("2 digits: " + num.ToString("X2"));
Console.WriteLine("4 digits: " + num.ToString("X4"));
Console.WriteLine("8 digits: " + num.ToString("X8"));
Console.WriteLine("\nLowercase with padding:");
Console.WriteLine("4 digits: " + num.ToString("x4"));
Console.WriteLine("6 digits: " + num.ToString("x6"));
}
}
The output of the above code is −
Default: FF 2 digits: FF 4 digits: 00FF 8 digits: 0000FF Lowercase with padding: 4 digits: 00ff 6 digits: 0000ff
Working with Different Data Types
Example
using System;
class Demo {
static void Main() {
byte b = 255;
short s = -1;
int i = 16777215;
long l = 281474976710655L;
Console.WriteLine("byte 255: " + b.ToString("X2"));
Console.WriteLine("short -1: " + s.ToString("X4"));
Console.WriteLine("int 16777215: " + i.ToString("X6"));
Console.WriteLine("long value: " + l.ToString("X12"));
Console.WriteLine("\nUsing lowercase:");
Console.WriteLine("RGB color: #" + i.ToString("x6"));
}
}
The output of the above code is −
byte 255: FF short -1: FFFF int 16777215: FFFFFF long value: FFFFFFFFFFFF Using lowercase: RGB color: #ffffff
Common Use Cases
-
Color values: Converting RGB values to hexadecimal for web colors (e.g., #FF0000 for red).
-
Memory addresses: Displaying memory locations in hexadecimal format.
-
Binary data: Representing byte arrays or binary data in readable hexadecimal format.
-
Debugging: Viewing numeric values in hexadecimal for easier bit manipulation analysis.
Conclusion
The hexadecimal format specifier "X" converts numbers to hexadecimal strings, with "X" producing uppercase letters and "x" producing lowercase letters for digits A-F. Precision specifiers can be added to control minimum digit count with zero-padding when needed.
