In C#, converting integers or other data types to hexadecimal strings can be usedToString
Method and specify the formatted string as"X"
or"x"
. The following are detailed implementation methods and examples:
1. Convert integers to hexadecimal strings
useToString
Method and specify the formatted string as"X"
(capital hexadecimal) or"x"
(lowercase hexadecimal).
Sample code:
using System; class Program { static void Main() { int number = 29; // Decimal number string hexUpper = ("X"); // capital hexadecimal string hexLower = ("x"); // Lowercase hexadecimal (hexUpper); // Output: 1D (hexLower); // Output: 1d } }
2. Specify the length of the hexadecimal string
If a fixed-length hexadecimal string is required, you can specify the length in the formatted string. For example,"X4"
Indicates that a 4-bit capital hexadecimal string is generated, and the insufficient part is filled with leading zeros.
Sample code:
using System; class Program { static void Main() { int number = 29; string hexFixedLength = ("X4"); // 4-bit capital hexadecimal (hexFixedLength); // Output: 001D } }
3. Convert byte array to hexadecimal string
If you need to add an array of bytes (byte[]
) Convert to hexadecimal string, can be usedBitConverter
Class or manually implemented.
Using BitConverter:
using System; class Program { static void Main() { byte[] bytes = { 0x1D, 0x2A, 0x3F }; string hexString = (bytes).Replace("-", ""); (hexString); // Output: 1D2A3F } }
Manual implementation:
using System; using ; class Program { static void Main() { byte[] bytes = { 0x1D, 0x2A, 0x3F }; StringBuilder hexBuilder = new StringBuilder(); foreach (byte b in bytes) { (("X2")); // Convert each byte to 2-bit hexadecimal } string hexString = (); (hexString); // Output: 1D2A3F } }
4. Convert characters in a string to hexadecimal
If you need to convert each character in the string to its hexadecimal representation, you can use the following method:
Sample code:
using System; using ; class Program { static void Main() { string input = "Hello"; StringBuilder hexBuilder = new StringBuilder(); foreach (char c in input) { (((int)c).ToString("X2") + " "); // Convert each character to 2-bit hexadecimal } string hexString = ().Trim(); (hexString); // Output: 48 65 6C 6C 6F } }
5. Convert hexadecimal string back to integer
If you need to convert a hexadecimal string back to an integer, you can useConvert.ToInt32
ormethod.
Sample code:
using System; class Program { static void Main() { string hexString = "1D"; // Hexadecimal string int number = Convert.ToInt32(hexString, 16); // Convert to integer (number); // Output: 29 } }
6. Summary
- use
ToString("X")
orToString("x")
Convert integers to hexadecimal strings. - use
ToString("X4")
Generate a fixed-length hexadecimal string. - use
BitConverter
Or manually implement converting a byte array into a hexadecimal string. - use
Convert.ToInt32
orConvert hexadecimal string back to an integer.
With these methods, it is easy to convert between hexadecimal and other binary systems in C#.
This is the article about how to convert to hexadecimal to C#. For more related content on converting to hexadecimal to C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!