From 3b9965554b6e08afc13ba24f2798194256581d63 Mon Sep 17 00:00:00 2001 From: lxw Date: Fri, 10 May 2024 14:40:35 +0800 Subject: [PATCH] =?UTF-8?q?decode=20/=20encode=20=E7=BC=96=E8=A7=A3?= =?UTF-8?q?=E7=A0=81=20=E5=85=B3=E4=BA=8E=20byte[]=20=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E7=BB=B4=E6=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkCore/Utils/BitUtls.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/HybirdFrameworkCore/Utils/BitUtls.cs b/HybirdFrameworkCore/Utils/BitUtls.cs index 8f652f0..a6034d7 100644 --- a/HybirdFrameworkCore/Utils/BitUtls.cs +++ b/HybirdFrameworkCore/Utils/BitUtls.cs @@ -18,6 +18,7 @@ public static class BitUtls private static readonly Type FLOAT = typeof(float); private static readonly Type DOUBLE = typeof(double); private static readonly Type STRING = typeof(string); + private static readonly Type BYTEARRAY = typeof(byte[]); #endregion @@ -211,6 +212,7 @@ public static class BitUtls public static object Bytes2Value(byte[] bytes, Type propertyType, int start, int length, double scale, int round,double offset) { + if (propertyType == BOOLEAN) { return Convert.ChangeType(Byte2Bit(bytes, start, length) == 1, propertyType); @@ -255,6 +257,18 @@ public static class BitUtls { return Convert.ChangeType(BytesToHexStr(bytes, start, length), propertyType); } + else if (propertyType == BYTEARRAY) + { + length = length / 8; + byte[] bt = new byte[length]; + + for (int i = start; i < (start + length); i++) + { + bt[i - start] = bytes[i]; + } + + return bt; + } throw new ArgumentException($"参数类型{propertyType}不支持encode!"); } @@ -320,6 +334,11 @@ public static class BitUtls return Encoding.ASCII.GetBytes(s); } } + if (propertyType == BYTEARRAY) + { + var bytes = value as byte[]; + return bytes; + } throw new ArgumentException($"参数类型{propertyType}不支持encode!"); }