/// <summary> /// 设置2个字节的数组中的某个位 /// </summary> /// <param name="bt"></param> /// <param name="bit"></param> /// <param name="val"></param> /// <param name="dataFormat"></param> /// <returns></returns> public static ushort SetbitValueFrom2ByteArray(byte[] bt, int bit, bool val, DataFormat dataFormat = DataFormat.ABCD) { 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> /// 设置16位整型某个位 /// </summary> /// <param name="value"></param> /// <param name="bit"></param> /// <param name="val"></param> /// <param name="dataFormat"></param> /// <returns></returns> public static short SetbitValueFromShort(short value, int bit, bool val, DataFormat dataFormat = DataFormat.ABCD) { byte[] bt = ByteArrayLib.GetByteArrayFromShort(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(GetShortFromByteArray(bt, 0, dataFormat)); }
/// <summary> /// 将布尔数组转换成字节数组 /// </summary> /// <param name="val">布尔数组</param> /// <returns>字节数组</returns> public static byte[] GetByteArrayFromBoolArray(bool[] val) { if (val == null && val.Length == 0) { return(null); } byte[] result = new byte[val.Length % 8 != 0 ? val.Length / 8 + 1 : val.Length / 8]; //遍历每个字节 for (int i = 0; i < result.Length; i++) { int total = val.Length < 8 * (i + 1) ? val.Length - 8 * i : 8; //遍历当前字节的每个位赋值 for (int j = 0; j < total; j++) { result[i] = ByteLib.SetbitValue(result[i], j, val[8 * i + j]); } } return(result); }
/// <summary> /// 返回字节数组中的某个字节某个位 /// </summary> /// <param name="b">字节数组</param> /// <param name="index">字节索引</param> /// <param name="offset">指定位(0-7)</param> /// <returns>布尔结果</returns> public static bool GetBitFromByteArray(byte[] b, int index, int offset) { byte res = ByteLib.GetByteFromByteArray(b, index); return(GetBitFromByte(res, offset)); }
/// <summary> /// 字节数组转换 /// </summary> /// <param name="src"></param> /// <param name="value"></param> /// <param name="start"></param> /// <param name="offset"></param> /// <returns></returns> public static byte[] SetByteArray(byte[] src, object value, int start, int offset) { string name = value.GetType().Name; byte[] b = null; switch (name.ToLower()) { case "boolean": Array.Copy(GetByteArrayFromByte(ByteLib.SetbitValue(src[start], offset, Convert.ToBoolean(value))), 0, src, start, 1); break; case "byte": Array.Copy(GetByteArrayFromByte(Convert.ToByte(value)), 0, src, start, 1); break; case "int16": Array.Copy(GetByteArrayFromShort(Convert.ToInt16(value)), 0, src, start, 2); break; case "uint16": Array.Copy(GetByteArrayFromUShort(Convert.ToUInt16(value)), 0, src, start, 2); break; case "int32": Array.Copy(GetByteArrayFromInt(Convert.ToInt32(value)), 0, src, start, 4); break; case "uint32": Array.Copy(GetByteArrayFromUInt(Convert.ToUInt32(value)), 0, src, start, 4); break; case "single": Array.Copy(GetByteArrayFromFloat(Convert.ToSingle(value)), 0, src, start, 4); break; case "double": Array.Copy(GetByteArrayFromDouble(Convert.ToDouble(value)), 0, src, start, 8); break; case "int64": Array.Copy(GetByteArrayFromLong(Convert.ToInt64(value)), 0, src, start, 8); break; case "uint64": Array.Copy(GetByteArrayFromULong(Convert.ToUInt64(value)), 0, src, start, 8); break; case "byte[]": b = GetByteArrayFromHexString(value.ToString()); Array.Copy(b, 0, src, start, b.Length); break; case "int16[]": b = GetByteArrayFromShortArray(ShortLib.GetShortArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; case "uint16[]": b = GetByteArrayFromUShortArray(UShortLib.GetUShortArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; case "int32[]": b = GetByteArrayFromIntArray(IntLib.GetIntArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; case "uint32[]": b = GetByteArrayFromUIntArray(UIntLib.GetUIntArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; case "single[]": b = GetByteArrayFromFloatArray(FloatLib.GetFloatArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; case "double[]": b = GetByteArrayFromDoubleArray(DoubleLib.GetDoubleArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; case "int64[]": b = GetByteArrayFromLongArray(LongLib.GetLongArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; case "uint64[]": b = GetByteArrayFromULongArray(ULongLib.GetULongArrayFromString(value.ToString())); Array.Copy(b, 0, src, start, b.Length); break; default: break; } return(src); }