visual studio c# realizes the code of conversion between hexadecimal and string

Keywords: C# visualstudio

Reprint: original website https://www.cnblogs.com/maanshancss/p/4074524.html
Reprint: original website https://www.cnblogs.com/maanshancss/p/4074524.html
Reprint: original website https://www.cnblogs.com/maanshancss/p/4074524.html
c# realize the conversion code between hexadecimal and string
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 the hexadecimal string.
Converts a hexadecimal string to an integer.
Converts a hexadecimal string to a floating-point type.
Converts a byte array to a hexadecimal string.

Example 1: output the hexadecimal value of each character in string.

First, it parses the string into an array of characters, and then calls ToInt32(Char) for each character to get the corresponding numeric value. Finally, set the format of the number to hexadecimal representation in string.

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

Example 2: analyze the string of hexadecimal value and output the character corresponding to each hexadecimal value

First, it calls Split(array) []) method to get each hexadecimal value as a single string in the array. Then ToInt32(String, Int32) is called to convert the sixteen decimal system to the decimal value represented as int. The example demonstrates two different methods for obtaining the characters corresponding to the character code. The first method is to use convertfromutf32 , which returns the character corresponding to the integer parameter as a string. The second method is to explicitly convert int to char.

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 !
*/
Example 3: demonstrates another method of converting hexadecimal string to integer, that is, calling Parse(String, NumberStyles) method.

string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);

//Output: 2274
Example 4: demonstrates how to use the System...::. BitConverter class and the Int32...::. Parse method to convert a hexadecimal string to a floating-point type.

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
Example 5: the following example demonstrates 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
*/
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 a string represented in hexadecimal
{
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 */

Author: Wang Siming
source: http://www.cnblogs.com/maanshancss/
The copyright of this article belongs to the author and the blog park. Reprint is welcome, but this statement must be retained without the consent of the author, and the original connection must be given in an obvious position on the article page, otherwise the right to investigate legal responsibility is reserved. All source codes follow the Apache protocol, and from MAANSHAN CSS must be added

Posted by hanhao on Fri, 17 Sep 2021 07:06:04 -0700