转换协议字节

 

转换协议字节

    /// <summary>
    /// 转换协议字节帮助类
    /// </summary>
    public class ByteUtil
    {
        /// <summary>
        /// byte数组转换int
        /// </summary>
        /// <param name="src"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public static int BytesToInt(byte[] src, int offset)
        {
            int value;
            value = (int)((src[offset] & 0xFF)
                    | ((src[offset + 1] & 0xFF) << 8)
                    | ((src[offset + 2] & 0xFF) << 16)
                    | ((src[offset + 3] & 0xFF) << 24));
            return value;
        }

        /// <summary>
        /// int转byte数组
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static byte[] IntToBytes(int value)
        {
            byte[] src = new byte[4];
            src[3] = (byte)((value >> 24) & 0xFF);
            src[2] = (byte)((value >> 16) & 0xFF);
            src[1] = (byte)((value >> 8) & 0xFF);
            src[0] = (byte)(value & 0xFF);
            return src;
        }
    }
View Code

 

 

https://www.cnblogs.com/hejiale010426/p/16924024.html

posted @ 2024-06-14 10:13  bxzjzg  阅读(4)  评论(0编辑  收藏  举报