C# 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;//from w w w . j  a  v a  2s  .com

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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var