Conversion between a hexadecimal string and a numeric value (C # programming guide)
The following example shows how to perform the following tasks:
Returns the hexadecimal value of each character in a string.
Returns the character corresponding to each value in a hexadecimal string.
Converts a hexadecimal string to an integer.
Converts a hexadecimal string to a floating point.
Converts a byte array to a hexadecimal string.
Example
In this example, the hexadecimal value of each character in the string is output. First, it analyzes the string as a character array, and then calls ToInt32 (Char) for each character to obtain the corresponding numeric value. Finally, set the number format to the hexadecimal representation in string.
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 of 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! Is 21
*/
This example analyzes the string of the hexadecimal value and outputs the characters corresponding to each hexadecimal value. First, it calls the Split (array <Char> [] () []) method to obtain each hexadecimal value as a single string in the array. Then, ToInt32 (String, Int32) is called to convert the hexadecimal format to the decimal value represented as int. The example demonstrates two different methods for obtaining characters corresponding 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 = "48 65 6C 6C 6F 20 57 6F 72 6C 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 = 48, int value = 72, char value = H or H
Hexadecimal value = 65, int value = 101, 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 = 111, char value = o or o
Hexadecimal value = 20, int value = 32, char value = or
Hexadecimal value = 57, int value = 87, char value = W or W
Hexadecimal value = 6F, int value = 111, char value = o or o
Hexadecimal value = 72, int value = 114, char value = r or r
Hexadecimal value = 6C, int value = 108, char value = l or l
Hexadecimal value = 64, int value = 100, char value = d or d
Hexadecimal value = 21, int value = 33, char value =! Or!
*/
This example demonstrates another way to convert a hexadecimal string to an integer, that is, to call 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 the System...:. BitConverter class and int32...:. Parse method to convert a hexadecimal string to a floating point type.
C #
String hexstrings = "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 the System...:. BitConverter class to convert 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
*/
It's just a pirated version on msdn!
Copy codeCode: public string StrToHex (string mStr) // return the processed hexadecimal string
{
Return BitConverter. ToString (
ASCIIEncoding. Default. GetBytes (mStr). Replace ("-","");
}/* StrToHex */
Public string HexToStr (string mHex) // returns the string in hexadecimal notation.
{
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 */