基本信息
源码名称:CRC验证
源码大小:0.32M
文件格式:.rar
开发语言:C#
更新时间:2016-05-26
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

modbus CRC验证

using System;
using System.Collections.Generic;

using System.Text;


namespace BasicTool
{
    /// <summary>
    ///  数值转换类
    /// </summary>
    public class NumberConvert
    {
        public static string timeCheckToHexStr(double timecheck)
        {
            string str = "";
            int t = Convert.ToInt32(timecheck*10);            
            str = String.Format("{0:X}", t);
            str = str.Substring(str.Length - 2, 2);            
            return str;
        }
        /// <summary>
        /// 整形数据(时间间隔) 转成 16进制字符串
        /// </summary>
        /// <param name="sctime"></param>
        /// <returns></returns>
        public static string scTimeToStr(int sctime)
        {
            string strsctime = String.Format("{0:X}", sctime);
            strsctime = "000" strsctime;
            string returestr;
            returestr = strsctime.Substring(strsctime.Length - 4, 4);
            returestr = strsctime.Substring(strsctime.Length - 4, 2) " ";
            returestr = strsctime.Substring(strsctime.Length - 2, 2) " ";
            return returestr;
        }
        /// <summary>
        /// 整形数据(端口号)转换成 16进制字符串 
        /// </summary>
        /// <param name="ipport"></param>
        /// <returns></returns>
        public static string ipPortToStr(int ipport)
        {
            string returestr = "";
            string stripport = String.Format("{0:X}", ipport);
            if (stripport.Length < 4)
            {
                returestr = "0" stripport.Substring(0, 1) " ";
                returestr = stripport.Substring(1, 2) " ";
            }
            else
            {
                returestr =  stripport.Substring(0, 2) " ";
                returestr = stripport.Substring(2, 2) " ";
            }
            return returestr;

        }
        /// <summary>
        /// ip地址 转换成 16进制 字符串
        /// </summary>
        /// <param name="ipaddress"></param>
        /// <returns></returns>
        public static string ipAddressToStr(string ipaddress)
        {
            string str = "";
            string[] s = ipaddress.Split('.');         
            for(int i=0;i<s.Length;i )
            {
                if(Convert.ToInt16(s[i])<16)
                {
                    str = "0" String.Format("{0:X}", Convert.ToInt32(s[i])) " ";                  
                }
                else { 
                    str =  String.Format("{0:X}", Convert.ToInt32(s[i])) " ";
                }
                
            }            
            return str;
        }
        /// <summary>
        /// int类型 转换成 byte数组 配合crc校验使用
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] intToHexByte(Int16 num)
        {                   
            byte[] b1 = BitConverter.GetBytes(num);           
            return b1;
        }
        /// <summary>
        /// double类型 转换成 byte 数组
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] doubleToHexByte(double num)
        {
            byte[] b = new byte[2];           
            Int16 t = Convert.ToInt16(num);
            byte[] b1 = BitConverter.GetBytes(t);
            b[0] = b1[1];
            b[1] = b1[0];
            return b;
        }
        /// <summary>
        /// 字节数组转16进制字符串 
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i )
                {
                    returnStr = bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }
        /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString = " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i )
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }    
    }
}