wangxinhui
2024-11-06 8f392cc88b0768b74efca3b68785cf5aa1c38e70
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace WIDESEA_Common.Tools
{
    public class DataTrans
    {
        /// <summary>
        /// 字节转bit字符串(注意偏移量0,对应的是下标7)
        /// </summary>
        /// <param name="b">字节</param>
        /// <returns></returns>
        public static char[] Byte2Bit(byte b)
        {
            string strTemp = Convert.ToString(b, 2).PadLeft(8, '0');
            return strTemp.ToCharArray();
        }
 
        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="socure">源</param>
        /// <param name="start">起始位置</param>
        /// <returns></returns>
        public static string GetStr(byte[] socure, int start)
        {
            //前两位是西门标识位
            //var len = (short)socure[start + 1];
            int len = 15;
            if (socure.Length < 15)
            {
                len = socure.Length;
            }
            List<byte> newByte = new List<byte>();
            for (int i = 0; i < 15; i++)
            {
                newByte.Add(socure[start + i + 2]);
            }
            return Encoding.ASCII.GetString(newByte.ToArray()).Replace("\0", "");
        }
    }
}