C# Hexadecimal ("X") Format

In this chapter you will learn:

  1. Hexadecimal ("X") Format Specifier
  2. Example to use Hexadecimal ("X") Format Specifier

Description

The hexadecimal ("X") specifier formats a number to a string of hexadecimal digits. The case specifier indicates whether to use uppercase or lowercase characters. For example, use "X" to produce "ABCDEF", and "x" to produce "abcdef".

Item Value
Name Hexadecimal
Format specifier"X" or "x"
Supported by Integral types only.
Result A hexadecimal string.
Precision specifier indicates the minimum number of digits desired in the resulting string.

Example,

ValueFormat Formatted
255 ("X") FF
-1 ("x") ff
255 ("x4") 00ff
-1 ("X4") 00FF

The result string is not affected by the NumberFormatInfo object.

Example

The following example formats Int32 values with the hexadecimal format specifier.


using System;/* ww  w  .  jav a  2 s .  c om*/

class MainClass
{
   static void Main()
   {
        int value = 12345;
        Console.WriteLine(value.ToString("x"));
        
        Console.WriteLine(value.ToString("X"));
        Console.WriteLine(value.ToString("X8"));
        
        
        value = 123456789;
        Console.WriteLine(value.ToString("X"));
        Console.WriteLine(value.ToString("X2"));
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to use Numeric ("N") Format Specifier
  2. Example to use Numeric ("N") Format Specifier
Home »
  C# Tutorial »
    C# Language »
      C# Standard Data Type Format
C# Standard Numeric Format
C# Currency ("C") Format
C# Decimal ("D") Format
C# Exponential ("E") Format
C# Fixed-Point ("F") Format
C# General ("G") Format
C# Hexadecimal ("X") Format
C# Numeric ("N") Format
C# Percent ("P") Format
C# Round-trip ("R") Format