C# 十进, HEX和ASCII中的转换--byte[]类型string类型

1 将整个ASCII类型的数组(byte[])解码为(string) byte[0]=65 byte[1]=66 string=AB

Encoding.ASCII.GetString(receive_buf)  

2.将整个ASCII类型的数组(byte[])解码为科学计数法(decimal)
s[0] =57;s[1] = 46;s[2] = 57;s[3] = 57;s[4] = 57; s[7] = 69;s[8] = 45;s[9] = 48;s[10] =51; decimal= 0.00999999

Convert.ToDecimal(Convert.ToDouble(Encoding.ASCII.GetString(receive_buf)))

3 1位 byte[]型转10进制int byte[0]=65 int=65

     public static int bytesToInt1(byte[] src, int offset) 
        {//src为byte数组,offset从数组的第office位开始
            int value;
            value = (int)(
                ((src[offset ] & 0xFF))
              );
            return value;
        }

4 2位 byte[]型转10进制int s[0] =57;s[1] = 46; int=14638

        public static int bytesToInt2(byte[] src, int offset)
        {//src为byte数组,offset从数组的第office位开始
            int value;
            value = (int)(
                ((src[offset + 1] & 0xFF) )
               | ((src[offset] & 0xFF) << 8));
            return value;
        }

5 4位 byte[]型转10进制int s[0] =57;s[1] = 46;s[2] = 57;s[3] = 57; int= 959330617

  public static int bytesToInt4(byte[] src, int offset)
  
        {//src为byte数组,offset从数组的第office位开始
            int value;
            value = (int)((src[offset+3] & 0xFF)
                | ((src[offset +2] & 0xFF) << 8)
               | ((src[offset + 1] & 0xFF) <<16)
               | ((src[offset ] & 0xFF) << 24));
            return value;
        }

6 16进制byte[]转16进制string(BCD) s[0] =57;s[1] = 46;s[2] = 57;s[3] = 57;s[4] = 57; 39 2E 39 39 39 39 39

public static string byteToHex(byte[] Byte)
        {
            string hex = string.Empty;
            StringBuilder strB = new StringBuilder();
            if (Byte != null)
            {
                foreach(byte b in Byte)
                {
                    strB.Append(b.ToString("X2")+" ");
                }             
            }
            hex = strB.ToString();
            return hex;
        }

7 16进制StringsTo16进制byte[] 39 2E 39 39 39 39 39 s[0] =57;s[1] = 46;s[2] = 57;s[3] = 57;s[4] = 57;

public static string send_data;
        public static byte[] StringsToHexbytes(string text)
        {
            byte[] temp = new byte[1];
            text = text.Replace(" ", "");
            string buf = text;
            string pattern = @"\s";
            string replacement = "";
            Regex rgx = new Regex(pattern);
             send_data = rgx.Replace(buf, replacement);
            int num=0;
            num = text.Length  / 2;
            byte[] return_data = new byte[num];
            for (int i = 0; i < num; i++)
            {
                temp[0] = Convert.ToByte(send_data.Substring(i * 2, 2).Trim(), 16);
                return_data[i] = temp[0];
            }
            return return_data;
        }

8 10进制整数转byte

  public static byte[] intToBytes(int hexint)
        {
            string HexString = Convert.ToString(hexint, 16);
            HexString = HexString.Replace(" ", "");
            if ((HexString.Length % 2) != 0) HexString = "0" + HexString;
            byte[] returnBytes = new byte[HexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(HexString.Substring(i * 2, 2).Trim(), 16);
            }
            return returnBytes;
        }

9 string转成转换成CRC码(byte[])

    public static byte[] CRC16Calc(string data)
        {
            string[] datas = data.Split('-');
            //string[] datas=null;
            //int j = 0;
            //for (int i= 0; i < data.Length; i += 2)
            //{
            //    datas[j] = data.Substring(0,2);
            //    j ++;
            //}

            List<byte> bytedata = new List<byte>();
            foreach (string str in datas)
            {
                bytedata.Add(byte.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier));
            }
            byte[] crcbuf = bytedata.ToArray(); //计算并填写CRC校验码 
            int crc = 0xffff;
            int len = crcbuf.Length;
            for (int n = 0; n < len; n++)
            {
                byte i; crc = crc ^ crcbuf[n];
                for (i = 0; i < 8; i++)
                {
                    int TT;
                    TT = crc & 1;
                    crc = crc >> 1;
                    crc = crc & 0x7fff;
                    if (TT == 1)
                    {
                        crc = crc ^ 0xa001;
                    }
                    crc = crc & 0xffff;
                }
            }
            byte[] redata = new byte[2];
            redata[1] = Convert.ToByte((byte)((crc >> 8) & 0xff));
            redata[0] = Convert.ToByte((byte)((crc & 0xff)));
            return redata;
        }

10 将的数组(byte[])转为hex的字符串 byte[0]=1 byte[1]=11 string=01 0B

  public static string ToHexString(byte[] bytes)
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    strB.Append(" " + bytes[i].ToString("X2"));
                }
                hexString = strB.ToString();

            }
            return hexString;
        }

你可能感兴趣的:(嵌入式物联通讯,c#)