converting between hexadecimal strings and numeric types (C # Programming Guide)
The following example shows how to perform the following tasks:
Gets the hexadecimal value of each character in the string.
Gets the character that corresponds to each value in the hexadecimal string.
Converts a hexadecimal string to an integral type.
Converts a hexadecimal string to a floating-point type.
Converts a byte array to a hexadecimal string.
Example
This example prints the hexadecimal value of each character in string. First, it analyzes string as a character array, and then calls ToInt32 (Char) on each character to get the corresponding numeric value. Finally, the format of the number in string is formatted as a hexadecimal representation.
C#
string input = "Hello world!";
Char[] values = input. ToCharArray ();
foreach (char letter in values)
{
Get the integral value of the character.
int value = Convert.ToInt32 (letter);
Convert the decimal value to a hexadecimal value in string form.
String hexoutput = String.Format ("{0:x}", value);
Console.WriteLine ("Hexadecimal value of {0} is {1}", letter, Hexoutput);
}
/* Output:
Hexadecimal value of H is 48
Hexadecimal value of E is 65
Hexadecimal value of L is 6C
Hexadecimal value of L is 6C
Hexadecimal value of O is 6F
Hexadecimal value is 20
Hexadecimal value of W is 57
Hexadecimal value of O is 6F
Hexadecimal value of R is 72
Hexadecimal value of L is 6C
Hexadecimal value of D is 64
Hexadecimal value of! is 21
*/
This example analyzes a string of hexadecimal values and outputs a character corresponding to each hexadecimal value. First, it calls the Split (array<char>[] []) method to get each hexadecimal value as a single string in the array. The ToInt32 (String, Int32) is then invoked to convert the 16 binary to a decimal value represented as int. The example shows two different ways to get the characters that correspond to the character code. The first method is to use CONVERTFROMUTF32 (INT32), which returns the character corresponding to the integer parameter as a string. The second method is to explicitly convert int to char.
C#
String hexvalues = "6C 6C 6F 6C 6F 64 21";
string[] Hexvaluessplit = Hexvalues.split (");
foreach (String hex in Hexvaluessplit)
{
Convert the number expressed in base-16 to an integer.
int value = Convert.ToInt32 (hex, 16);
Get the character corresponding to the integral value.
String stringvalue = Char.convertfromutf32 (value);
Char charvalue = (char) value;
Console.WriteLine ("Hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
Hex, value, StringValue, charvalue);
}
/* Output:
Hexadecimal value = +, int value =, char value = h or H
Hexadecimal value = +, int value = d, char value = e or E
Hexadecimal value = 6C, int value = 108, char value = L or L
Hexadecimal value = 6C, int value = 108, char value = L or L
Hexadecimal value = 6F, int value = A, char value = O or O
hexadecimal value =, int value =, char value = or
Hexadecimal value = m, int value =, char value = W or W
Hexadecimal value = 6F, int value = A, char value = O or O
Hexadecimal value = A, int value = 114, char value = R or R
Hexadecimal value = 6C, int value = 108, char value = L or L
Hexadecimal value = $, int value = m, char value = d or D
hexadecimal value =, int value =, char value =! or!
*/
This example demonstrates another way to convert a hexadecimal string to an integer by calling the Parse (string, NumberStyles) method.
C#
String hexstring = "8E2";
int num = Int32.Parse (hexstring, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine (num);
output:2274
The following example shows how to use System..::. Bitconverter class and Int32..::. The Parse method converts a hexadecimal string to a floating-point type.
C#
String hexstring = "43480170";
UINT num = uint. Parse (hexstring, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatvals = bitconverter.getbytes (num);
float f = bitconverter.tosingle (floatvals, 0);
Console.WriteLine ("Float convert = {0}", f);
output:200.0056
The following example shows how to use System..::. The Bitconverter class converts a byte array to a hexadecimal string.
C#
Byte[] Vals = {0x01, 0xAA, 0xb1, 0xDC, 0x10, 0xDD};
String str = bitconverter.tostring (vals);
Console.WriteLine (str);
str = bitconverter.tostring (vals). Replace ("-", "");
Console.WriteLine (str);
/*output:
01-aa-b1-dc-10-dd
01aab1dc10dd
*/
Just the piracy on MSDN!
Copy Code code as follows:
public string Strtohex (String mstr)//Returns the processed hexadecimal string
{
Return bitconverter.tostring (
ASCIIEncoding.Default.GetBytes (MSTR)). Replace ("-", "");
}/* Strtohex * *
public string Hextostr (string mhex)//Returns a hexadecimal-represented string
{
Mhex = Mhex.replace ("", "");
if (mhex.length <= 0) return "";
byte[] vbytes = new BYTE[MHEX.LENGTH/2];
for (int i = 0; i < mhex.length i + + 2)
if (!byte. TryParse (Mhex.substring (i, 2), Numberstyles.hexnumber, NULL, out VBYTES[I/2))
VBYTES[I/2] = 0;
Return ASCIIEncoding.Default.GetString (vbytes);
}/* HEXTOSTR * *