16 Jun

C# Convert A Numeric Value To Hexadecimal Format

This example shows a numeric value in its hexadecimal format. To do this, we use the x or X formatting specifier. The letter case of the formatting specifier determines whether the hexadecimal letters appear in lowercase or uppercase.

Sample Code 1:

            // Convert numeric value to hexadecimal value
            Console.WriteLine(string.Format("0x{0:X}", 12));
            Console.WriteLine(string.Format("0x{0:X}", 64));
            Console.WriteLine(string.Format("0x{0:X}", 123));
            Console.WriteLine(string.Format("0x{0:X}", 196));
            Console.WriteLine(string.Format("0x{0:X}", 255));

If we want to convert hexadecimal value to numeric value, we can use like below:

Sample Code 2:

            //Convert hexadecimal value to numeric value
            Console.WriteLine(Convert.ToInt32("0xC", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0x40", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0x7B", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0xC4", 16).ToString());
            Console.WriteLine(Convert.ToInt32("0xFF", 16).ToString());

Leave a Reply

Your email address will not be published. Required fields are marked *