C#進位制轉換操作(三)之16進位制操作
阿新 • • 發佈:2019-01-31
一、字串和16進位制字串互轉
1.字串轉16進位制字串
/// <summary> /// 字串轉Hex,方法1使用Convert.ToString(byte, 16) /// </summary> /// <param name="str"></param> /// <returns></returns> public static string StringToHex(string str) { StringBuilder sb = new StringBuilder(); byte[] byStr = System.Text.Encoding.Default.GetBytes(str); //預設是System.Text.Encoding.Default.GetBytes(str) for (int i = 0; i < byStr.Length; i++) { sb.Append(Convert.ToString(byStr[i], 16)); } return (sb.ToString().ToUpper()); } /// <summary> /// 字串轉Hex,方法2使用BitConverter /// </summary> /// <param name="mStr"></param> /// <returns></returns> public static string StrToHex(string mStr) //返回處理後的十六進位制字串 { return BitConverter.ToString( Encoding.Default.GetBytes(mStr)).Replace("-", " "); //return BitConverter.ToString( //ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " "); }
2.16進位制字串轉字串
使用例項:/// <summary> /// 返回十六進位制代表的字串 /// </summary> /// <param name="mHex"></param> /// <returns></returns> public static string HexToString(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 Encoding.Unicode.GetString(vBytes); //return Encoding.UTF8.GetString(vBytes); // return Encoding.Default.GetString(vBytes); //return Encoding.ASCII.GetString(vBytes); return Encoding.UTF7.GetString(vBytes); // return ASCIIEncoding.Default.GetString(vBytes); }
/*
* 特別說明:字串和十六進位制之間的轉換需要使用相同的字符集才能識別
*/
Console.WriteLine(StringToHex("張三ABC"));//D5C5C8FD414243
Console.WriteLine(StrToHex("張三ABC")); //D5 C5 C8 FD 41 42 43
Console.WriteLine(HexToString("D5C5C8FD414243")); //張三ABC
二、位元組陣列轉十六進位制字串,使用BitCoinverter類
/*Output:byte[] vals = { 0x01, 0xAA, 0xB1, 0xDC, 0x10, 0xDD }; string str = BitConverter.ToString(vals); Console.WriteLine(str); str = BitConverter.ToString(vals).Replace("-", ""); Console.WriteLine(str);
01-AA-B1-DC-10-DD
01AAB1DC10DD
*/
三、十六進位制 string 轉換為浮點型,轉換為整數
1.十六進位制字串轉整數
string hexString = "8E2";
int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Console.WriteLine(num);
//Output:
2274
2.十六進位制轉浮點數
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
四、字串和十六進位制字串操作
1.輸出字串中每個字元的十六進位制值
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
*/
2.輸出十六進位制字串值的字元
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 !
*/
更多: