Hexadecimal ('X') Format Specifier in C#
The hexadecimal ("X") format specifier in C# is used to represent a number as a hexadecimal string. The "X" or "x" format specifier is used to convert a number to a string (digit) in uppercase or lowercase hexadecimal, respectively.
C# program:
using System;
class Program
{
static void Main()
{
int num1 = 255;
int num2 = 2019;
// Uppercase hexadecimal
string hexUpper = num1.ToString("X");
string hexUpperWithPadding = num2.ToString("X6");
// Lowercase hexadecimal
string hexLower = num1.ToString("x");
string hexLowerWithPadding = num2.ToString("x6");
Console.WriteLine($"Number1 in uppercase hexadecimal: {hexUpper}");
Console.WriteLine($"Number2 in uppercase hexadecimal with padding: {hexUpperWithPadding}");
Console.WriteLine($"Number1 in lowercase hexadecimal: {hexLower}");
Console.WriteLine($"Number2 in lowercase hexadecimal with padding: {hexLowerWithPadding}");
Console.ReadKey();
}
}
Output:
Number1 in uppercase hexadecimal: FF
Number2 in uppercase hexadecimal with padding: 0007E3
Number1 in lowercase hexadecimal: ff
Number2 in lowercase hexadecimal with padding: 0007e3
Comments (0)