C# Hexadecimal ("X") Format
In this chapter you will learn:
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,
Value | Format | 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: