标签:
/// <summary> /// 将十六进制字符串转化为字节数组 /// </summary> /// <param name="src"></param> /// <returns></returns> public byte[] HexString2Bytes(string src) { byte[] retBytes = new byte[src.Length / 2]; for (int i = 0; i < src.Length / 2; i++) { retBytes[i] = byte.Parse(src.Substring(i * 2, 2), System.Globalization.NumberStyles.AllowHexSpecifier); } return retBytes; } /// <summary> /// 字符串转16进制 /// </summary> /// <param name="mHex"></param> /// <returns>返回十六进制代表的字符串</returns> public string HexToStr(string mHex,bool isuft8=false) // 返回十六进制代表的字符串 { if (isuft8 == false) { byte[] bTemp = System.Text.Encoding.Default.GetBytes(mHex); string strTemp = ""; for (int i = 0; i < bTemp.Length; i++) { strTemp += bTemp[i].ToString("X"); } return strTemp; } else { byte[] bTemp = System.Text.Encoding.UTF8.GetBytes(mHex); string strTemp = ""; for (int i = 0; i < bTemp.Length; i++) { strTemp += bTemp[i].ToString("X"); } return strTemp; } } /// <summary> /// 十六进制换算为十进制 /// </summary> /// <param name="strColorValue"></param> /// <returns></returns> public static int GetHexadecimalValue(String strColorValue) { char[] nums = strColorValue.ToCharArray(); int total = 0; try { for (int i = 0; i < nums.Length; i++) { String strNum = nums[i].ToString().ToUpper(); switch (strNum) { case "A": strNum = "10"; break; case "B": strNum = "11"; break; case "C": strNum = "12"; break; case "D": strNum = "13"; break; case "E": strNum = "14"; break; case "F": strNum = "15"; break; default: break; } double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - 1)); total += Convert.ToInt32(strNum) * Convert.ToInt32(power); } } catch (System.Exception ex) { String strErorr = ex.ToString(); return 0; } return total; }
标签:
原文地址:http://www.cnblogs.com/milest/p/5149991.html