ningshuxia
2022-11-22 4efe844d9bcc03435cbbeb1aedbda5bf6ebf5912
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.DataDockingSocket
{
    internal class ByteMsgHelper
    {
        /// <summary>
        /// 字符串转16进制字符2
        /// </summary>
        /// <param name="content">字符串</param>
        /// <param name="encode">编码格式</param>
        /// <returns></returns>
        public static List<byte> StringToHexValuve(string content, out string error_info)
        {
            //去掉空格
            string[] arr = content.Split(' ');
            if (arr.Length < 3)
            {
                arr = content.Split('-');
                if (arr.Length < 3)
                {
                    error_info = "字符无法解析";
                    return null;
                }
            }
            List<byte> result = new List<byte>();
            for (int i = 0; i < arr.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(arr[i]))
                    continue;
 
                var dddd = Convert.ToByte(arr[i], 16);
                result.Add(dddd);
            }
            error_info = null;
            return result;
        }
 
        #region 数据转化
        public static int GetInt2Byte(byte[] byteMessage, int startPosition, out string info)
        {
            byte[] value = new byte[2];
            Array.Copy(byteMessage, startPosition, value, 0, 2);
 
            info = BitConverter.ToString(value, 0, value.Length);
            // ushort rValueP = BitConverter.ToUInt16(value.Reverse().ToArray(), 0);
 
            //short rValueP = 0;
            //rValueP = byteMessage[  startPosition];
            //rValueP <<= 8;
            //rValueP += byteMessage[ startPosition + 1];
            //return rValueP;
            return bytesToInt2(byteMessage, startPosition);
        }
 
        public static int GetInt4Byte(byte[] byteMessage, int startPosition, out string info)
        {
            byte[] value = new byte[4];
            Array.Copy(byteMessage, startPosition, value, 0, 4);
 
            info = BitConverter.ToString(value, 0, value.Length);
            // ushort rValueP = BitConverter.ToUInt16(value.Reverse().ToArray(), 0);
 
            //short rValueP = 0;
            //rValueP = byteMessage[  startPosition];
            //rValueP <<= 8;
            //rValueP += byteMessage[ startPosition + 1];
            //return rValueP;
            return bytesToInt4(byteMessage, startPosition);
        }
        /** 
        * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
        *  
        * @param src 
        *            byte数组 
        * @param offset 
        *            从数组的第offset位开始 
        * @return int数值 
        */
        public static int bytesToInt2_LH(byte[] src, int offset)
        {
            int value;
            value = src[offset] & 0xFF
                    | (src[offset + 1] & 0xFF) << 8;
            return value;
        }
 
        /** 
        * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。2个字节
        */
        public static int bytesToInt2(byte[] src, int offset)
        {
            int value;
            value =
                     (src[offset + 0] & 0xFF) << 8
                    | src[offset + 1] & 0xFF;
            return value;
        }
        /** 
        * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。4个字节
        */
        public static int bytesToInt4(byte[] src, int offset = 0)
        {
            int value;
            value = (src[offset] & 0xFF) << 24
                    | (src[offset + 1] & 0xFF) << 16
                    | (src[offset + 2] & 0xFF) << 8
                    | src[offset + 3] & 0xFF;
            return value;
        }
 
        public static double GetDouble1(byte[] byteMessage, int startPosition, out string info)
        {
            byte[] value = new byte[4];
            Array.Copy(byteMessage, startPosition, value, 0, 4);
            info = BitConverter.ToString(value, 0, value.Length);
 
            //return BitConverter.ToDouble(byteMessage, startPosition);
            return BitConverter.ToSingle(value.Reverse().ToArray(), 0);//采用了IEEE-754二进制浮点数算术标准
        }
 
 
        #endregion
    }
}