十六進制字元串與數值類型之間轉換(C# 程式設計指南)
以下示例示範如何執行下列任務:
擷取字元串中每個字元的十六進制值。
擷取與十六進制字元串中的每個值對應的字元。
将十六進制 string 轉換為整型。
将十六進制 string 轉換為浮點型。
将位元組數組轉換為十六進制 string。
示例分析
此示例輸出 string 中的每個字元的十六進制值。首先,它将 string 分析為字元數組,然後對每個字元調用 ToInt32(Char) 以擷取相應的數字值。最後,在 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 o is 6F
Hexadecimal value of is 20
Hexadecimal value of W is 57
Hexadecimal value of r is 72
Hexadecimal value of d is 64
Hexadecimal value of ! is 21
十六進制值的 string 并輸出對應于每個十六進制值的字元。首先,它調用 Split(array<Char>[]()[]) 方法以擷取每個十六進制值作為數組中的單個 string。然後調用 ToInt32(String, Int32) 以将十六進制轉換為表示為 int 的十進制值。示例中示範了用于擷取對應于該字元代碼的字元的兩種不同方法。第一種方法是使用 ConvertFromUtf32(Int32),它将對應于整型參數的字元作為 string 傳回。第二種方法是将 int 顯式轉換為 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);

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 = 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 = 72, int value = 114, char value = r or r
hexadecimal value = 64, int value = 100, char value = d or d
hexadecimal value = 21, int value = 33, char value = ! or !
此示例示範了将十六進制 string 轉換為整數的另一種方法,即調用 Parse(String, NumberStyles) 方法。
string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);
Output: 2274
下面的示例示範如何使用 System..::.BitConverter 類和 Int32..::.Parse 方法将十六進制 string 轉換為浮點型。

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
下面的示例示範如何使用 System..::.BitConverter 類将位元組數組轉換為十六進制字元串。
C#代碼:

byte[] vals = { 0x01, 0xAA, 0xB1, 0xDC, 0x10, 0xDD };
string str = BitConverter.ToString(vals);
Console.WriteLine(str);
str = BitConverter.ToString(vals).Replace("-", "");

01-AA-B1-DC-10-DD
01AAB1DC10DD
隻是msdn上的盜版!代碼如下:

public string StrToHex(string mStr) //傳回處理後的十六進制字元串
return BitConverter.ToString(
ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " ");
/* StrToHex */
public string HexToStr(string mHex) // 傳回十六進制代表的字元串
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 */
本文轉自鋼鋼部落格園部落格,原文連結:http://www.cnblogs.com/xugang/archive/2011/11/14/2248307.html,如需轉載請自行聯系原作者