/// <summary>
        /// 将字节数组转换成带16进制字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <param name="segment"></param>
        /// <returns></returns>
        public static string GetHexStringFromByteArray(byte[] source, int start, int count, char segment = ' ')
        {
            byte[] b = ByteArrayLib.GetByteArray(source, start, count);

            StringBuilder sb = new StringBuilder();

            if (b.Length > 0)
            {
                foreach (var item in b)
                {
                    if (segment == 0)
                    {
                        sb.Append(string.Format("{0:X2}", item));
                    }
                    else
                    {
                        sb.Append(string.Format("{0:X2}{1}", item, segment));
                    }
                }
            }
            if (segment != 0 && sb.Length > 1 && sb[sb.Length - 1] == segment)
            {
                sb.Remove(sb.Length - 1, 1);
            }
            return(sb.ToString());
        }
        /// <summary>
        /// 返回字节数组中某2个字节的指定位
        /// </summary>
        /// <param name="b">字节数组</param>
        /// <param name="index">字节索引</param>
        /// <param name="offset">指定位(0-15)</param>
        /// <param name="reverse">字节顺序</param>
        /// <returns>布尔结果</returns>
        public static bool GetBitFrom2ByteArray(byte[] b, int index, int offset, bool reverse = false)
        {
            byte[] res = ByteArrayLib.GetByteArray(b, index, 2);

            if (res != null)
            {
                return(GetBitFrom2Byte(res, offset, reverse));
            }
            else
            {
                throw new Exception("请检查字节索引");
            }
        }
Example #3
0
        /// <summary>
        /// 设置UShort中的某个位
        /// </summary>
        /// <param name="value"></param>
        /// <param name="bit"></param>
        /// <param name="val"></param>
        /// <param name="dataFormat"></param>
        /// <returns></returns>
        public static ushort SetbitValueFromUShort(ushort value, int bit, bool val, DataFormat dataFormat = DataFormat.ABCD)
        {
            byte[] bt = ByteArrayLib.GetByteArrayFromUShort(value, dataFormat);

            if (bit >= 0 && bit <= 7)
            {
                bt[1] = ByteLib.SetbitValue(bt[1], bit, val);
            }
            else
            {
                bt[0] = ByteLib.SetbitValue(bt[0], bit - 8, val);
            }
            return(GetUShortFromByteArray(bt, 0, dataFormat));
        }
        /// <summary>
        /// 将字节数组转换成西门子字符串
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetSiemensStringFromByteArray(byte[] source, int start, int length)
        {
            byte[] b = ByteArrayLib.GetByteArray(source, start, length + 2);

            int valid = b[1];

            if (valid > 0)
            {
                return(Encoding.GetEncoding("GBK").GetString(ByteArrayLib.GetByteArray(b, 2, valid)));
            }
            else
            {
                return("empty");
            }
        }
 /// <summary>
 /// 字节数组中截取转成64位整型
 /// </summary>
 /// <param name="source"></param>
 /// <param name="start"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static ulong GetULongFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
 {
     byte[] b = ByteArrayLib.Get8ByteArray(source, start, type);
     return(b == null ? 0 : BitConverter.ToUInt64(b, 0));
 }
Example #6
0
 /// <summary>
 /// 将字节数组中某8个字节转换成Double类型
 /// </summary>
 /// <param name="source">字节数组</param>
 /// <param name="start">开始位置</param>
 /// <param name="type">字节顺序</param>
 /// <returns>Double类型数值</returns>
 public static double GetDoubleFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
 {
     byte[] b = ByteArrayLib.Get8ByteArray(source, start, type);
     return(b == null ? 0.0 : BitConverter.ToDouble(b, 0));
 }
Example #7
0
 /// <summary>
 /// 字节数组中截取转成16位无符号整型
 /// </summary>
 /// <param name="source"></param>
 /// <param name="start"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static ushort GetUShortFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
 {
     return(BitConverter.ToUInt16(ByteArrayLib.Get2ByteArray(source, start, type), 0));
 }
Example #8
0
        /// <summary>
        /// 从字节数组中截取某个字节
        /// </summary>
        /// <param name="source"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        public static byte GetByteFromByteArray(byte[] source, int start)
        {
            byte[] b = ByteArrayLib.GetByteArray(source, start, 1);

            return(b == null ? (byte)0 : b[0]);
        }
Example #9
0
 /// <summary>
 /// 字节数组中截取转成32位整型
 /// </summary>
 /// <param name="source"></param>
 /// <param name="start"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static int GetIntFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
 {
     byte[] b = ByteArrayLib.Get4ByteArray(source, start, type);
     return(b == null?0: BitConverter.ToInt32(b, 0));
 }
Example #10
0
 /// <summary>
 /// 将字节数组中某4个字节转换成Float类型
 /// </summary>
 /// <param name="source"></param>
 /// <param name="start"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static float GetFloatFromByteArray(byte[] source, int start = 0, DataFormat type = DataFormat.ABCD)
 {
     byte[] b = ByteArrayLib.Get4ByteArray(source, start, type);
     return(b == null ? 0.0f : BitConverter.ToSingle(b, 0));
 }
 /// <summary>
 /// 将字节数组转换成带编码格式字符串
 /// </summary>
 /// <param name="source"></param>
 /// <param name="start"></param>
 /// <param name="count"></param>
 /// <returns></returns>
 public static string GetStringFromByteArray(byte[] source, int start, int count)
 {
     return(Encoding.ASCII.GetString(ByteArrayLib.GetByteArray(source, start, count)));
 }
Example #12
0
 /// <summary>
 /// 字节数组中截取转成16位整型
 /// </summary>
 /// <param name="source"></param>
 /// <param name="start"></param>
 /// <param name="dataFormat"></param>
 /// <returns></returns>
 public static short GetShortFromByteArray(byte[] source, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
 {
     return(BitConverter.ToInt16(ByteArrayLib.Get2ByteArray(source, start, dataFormat), 0));
 }