c sharp implements hexadecimal and string conversion code

  • 2020-05-05 11:49:26
  • OfStack

Conversion between hexadecimal strings and numeric types (C# programming guide)
The following example demonstrates how to perform the following tasks:
Gets the hexadecimal value of each character in the string.
Gets the character corresponding to each value in a hexadecimal string.
Converts hexadecimal string to integer.
Converts hexadecimal string to floating point.
Converts the byte array to hexadecimal string.
Sample
This example outputs a hexadecimal value for each character in string. First, it parses string as an array of characters, and then calls ToInt32(Char) for each character to get the corresponding numeric value. Finally, in string, format the number 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 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 of ! is 21
*/
This example analyzes string of hexadecimal values and outputs the characters corresponding to each hexadecimal value. First, it calls Split(array<) Char > The []()[]) method gets each hexadecimal value as a single string in the array. ToInt32(String, Int32) is then called to convert hexadecimal to a decimal value represented as int. Two different methods for obtaining the characters corresponding to this character code are demonstrated in the example. The first is to use ConvertFromUtf32(Int32), which returns the character corresponding to the integer parameter as 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 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 classes and Int32.. ::.Parse method converts hexadecimal string to floating point.
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 piracy on msdn!

 
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 the hexadecimal 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 */ 


Related articles: