C# 负数和十六进制转换
1 /// <summary> 2 /// 十六进制转为负数 3 /// </summary> 4 /// <param name="strNumber"></param> 5 /// <returns></returns> 6 private static int HexStringToNegative(string strNumber) 7 { 8 int iNegate = 0; 9 int iNumber = Convert.ToInt32(strNumber, 16); 10 11 if (iNumber > 127) 12 { 13 short bbb = (short)~(iNumber - 1); 14 string bin = Convert.ToString(bbb, 2).PadLeft(16, \'0\'); 15 iNegate = -Convert.ToInt32(bin, 2); 16 } 17 return iNegate; 18 } 19 /// <summary> 20 /// 整数转负数的16进制 21 /// </summary> 22 /// <param name="Number"></param> 23 /// <returns></returns> 24 private static string NegativeToHexString(int Number) 25 { 26 short iComplement =(short) (~Number + 1); 27 string strResult = Convert.ToString(iComplement, 16).ToUpper(); 28 return strResult; 29 }