zoukankan      html  css  js  c++  java
  • 数据类型转换byteushotint64(long)

    /**
    * 64 00 64 00
    77 BE 9F 1A 2F DD 5E 40
    equipnum = 6553700
    equipnum = 6553700
    equipnum = 6553700
    equipnum = 6553700
    *
    * */
    private void button4_Click(object sender, EventArgs e)
    {
    //int i = 123;
    int i = 6553700;
    byte[] intBuff = BitConverter.GetBytes(i); // 将 int 转换成字节数组
    foreach (byte b in intBuff)
    {
    richTextBox1.AppendText(string.Format("{0} ", b.ToString("X2")));
    }
    i = BitConverter.ToInt32(intBuff, 0); // 从字节数组转换成 int
    richTextBox1.AppendText(" ");

            double x = 123.456;
            byte[] doubleBuff = BitConverter.GetBytes(x);  // 将 double 转换成字节数组
            foreach (byte b in doubleBuff)
            {
                richTextBox1.AppendText(string.Format("{0} ", b.ToString("X2")));
            }
            x = BitConverter.ToDouble(doubleBuff, 0);       // 从字节数组转换成 double
    
            // ushort数组 转 long(int64)
            // 1、移位
            ushort[] arr = new ushort[] { 100, 100, 0, 0 };
            Int64 equipNum = 0;
            for (int m = 0; m < 4; m++)
            {
                equipNum = equipNum | (arr[m] << m * 16);
            }
            richTextBox1.AppendText(string.Format("equipnum = {0}
    ", equipNum));
    
            // 2、使用BitConverter,先转为字节,在转为long
            byte[] by = new byte[8];
            for (int m = 0; m < 4; m++)
            {
                byte[] b = BitConverter.GetBytes(arr[m]);
                by[2*m] = b[0];
                by[2*m+1] = b[1];
            }
            equipNum = BitConverter.ToInt64(by, 0);
            richTextBox1.AppendText(string.Format("equipnum = {0}
    ", equipNum));
    
            byte[] bys = new byte[8];
            for (int m = 0; m < 4; m++)
            {
                byte[] b = BitConverter.GetBytes(arr[m]);
                Array.Copy(b, 0, bys, 2 * m, 2);
            }
            equipNum = BitConverter.ToInt64(bys, 0);
            richTextBox1.AppendText(string.Format("equipnum = {0}
    ", equipNum));
    
            byte[] bya = new byte[8];
            int k = 0;
            foreach (var a in arr)
            {
                byte[] b = BitConverter.GetBytes(a);
                Array.Copy(b, 0, bya, 2 * k, 2);
                k += 1;
            }
            equipNum = BitConverter.ToInt64(bya, 0);
            richTextBox1.AppendText(string.Format("equipnum = {0}
    ", equipNum));
        }
  • 相关阅读:
    输入设备驱动
    Windows下Eclipse+PyDev安装Python开发环境
    Wireshark does not show SSL/TLS
    error trying to exec 'cc1plus': execvp: 没有那个文件或目录
    json 的key值不能是变量
    获取url参数(jq 扩展包)
    jsonp 完成跨域请求注意事项
    怎样删除数据库表中所有的数据
    sqlserver中数据的四种插入方式
    DataGridView
  • 原文地址:https://www.cnblogs.com/windlog/p/12867449.html
Copyright © 2011-2022 走看看