Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Represent Int32 as a Hexadecimal String in C#
To represent Int32 as a hexadecimal string in C#, you can use several methods. The most common approaches are using Convert.ToString() with base 16, or using the ToString() method with hexadecimal format specifiers.
Int32 represents a 32-bit signed integer that can hold values from -2,147,483,648 to 2,147,483,647. Converting these values to hexadecimal format is useful for debugging, display purposes, and when working with low-level operations.
Using Convert.ToString() with Base 16
The Convert.ToString() method accepts a base parameter. Setting it to 16 converts the integer to its hexadecimal representation −
using System;
class Demo {
static void Main() {
int val = 9898;
Console.WriteLine("Integer: " + val);
Console.WriteLine("Hex String: " + Convert.ToString(val, 16));
// With negative numbers
int negVal = -1234;
Console.WriteLine("Negative Integer: " + negVal);
Console.WriteLine("Hex String: " + Convert.ToString(negVal, 16));
}
}
The output of the above code is −
Integer: 9898 Hex String: 26aa Negative Integer: -1234 Hex String: fffffb2e
Using ToString() with Format Specifiers
The ToString() method with format specifiers provides more control over the output format −
using System;
class Demo {
static void Main() {
int val = 255;
Console.WriteLine("Integer: " + val);
Console.WriteLine("Hex (lowercase): " + val.ToString("x"));
Console.WriteLine("Hex (uppercase): " + val.ToString("X"));
Console.WriteLine("Hex with padding: " + val.ToString("X8"));
Console.WriteLine("Hex with 0x prefix: 0x" + val.ToString("X"));
}
}
The output of the above code is −
Integer: 255 Hex (lowercase): ff Hex (uppercase): FF Hex with padding: 000000FF Hex with 0x prefix: 0xFF
Comparison of Methods
| Method | Format | Example Output | Use Case |
|---|---|---|---|
| Convert.ToString(val, 16) | Lowercase, no padding | 26aa | Simple conversion |
| val.ToString("x") | Lowercase, no padding | 26aa | Instance method call |
| val.ToString("X") | Uppercase, no padding | 26AA | Uppercase format |
| val.ToString("X8") | Uppercase, 8-digit padding | 000026AA | Fixed-width output |
Working with Large Numbers
Here's an example showing hexadecimal conversion for various integer ranges −
using System;
class Demo {
static void Main() {
int[] values = { 0, 16, 255, 4096, int.MaxValue, int.MinValue };
foreach (int val in values) {
Console.WriteLine($"Decimal: {val,12} | Hex: {val.ToString("X8")}");
}
}
}
The output of the above code is −
Decimal: 0 | Hex: 00000000 Decimal: 16 | Hex: 00000010 Decimal: 255 | Hex: 000000FF Decimal: 4096 | Hex: 00001000 Decimal: 2147483647 | Hex: 7FFFFFFF Decimal: -2147483648 | Hex: 80000000
Conclusion
Converting Int32 to hexadecimal strings in C# can be accomplished using Convert.ToString(val, 16) for simple conversion or ToString("X") format specifiers for more control over case and padding. Both methods handle the full range of 32-bit signed integers effectively.
