C#进制转换

十六进制字符串转换为十进制数字
直接用:
int i = int.Parse("aa", System.Globalization.NumberStyles.HexNumber);
iCheckNum = iCheckNum + Convert.ToInt32(strReceInfo.Substring(i,2),16);
十进制转十六进制 int i=100; string hstr = Convert.ToString(i, 16);

计算校验码
        /// <summary>
        /// 校验码
        /// </summary>
        /// <param name="strReceInfo"></param>
        /// <returns></returns>
        private Boolean  checkoutDataBuff(string strReceInfo)
        { 
              int iCheckNum = 0;  //校验码
             for (int i =0 ; i < strReceInfo .Length - 2; i = i+2)
            {
                 iCheckNum = iCheckNum + Convert.ToInt32(strReceInfo.Substring(i,2),16);
            }

             string strChkeckNum = Convert.ToString(iCheckNum, 16).PadLeft(2, '0');
             strChkeckNum = strChkeckNum.Substring(strChkeckNum.Length - 2, 2).ToUpper();
             if (strChkeckNum == strReceInfo.Substring(strReceInfo.Length - 2, 2))
                 return true;
             else
                 return false;
        }
二进制转十进制

int intLogType = Convert.ToInt32 (strBin.Substring(0, 4), 2);
十进制转二进制
string strBin = Convert .ToString(iBin,2);

posted @ 2012-10-30 18:47  秋官  阅读(190)  评论(0编辑  收藏  举报