Techiehook Techiehook
Updated date Aug 01, 2024
This article will provide information on how to use the hexadecimal ('X' or 'x') format specifier in C# to convert numbers to uppercase or lowercase hexadecimal strings.

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

ABOUT THE AUTHOR

Techiehook
Techiehook
Admin, Australia

Welcome to TechieHook.com! We are all about tech, lifestyle, and more. As the site admin, I share articles on programming, tech trends, daily life, and reviews... For more detailed information, please check out the user profile

https://www.techiehook.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!