• 数据类型转换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));
        }
  • 相关阅读:
    简单测试AF3.0.4
    好玩的Mac键盘
    黑盒测试和白盒测试
    iOS开发之原生二维码生成与扫描
    Swift
    JavaScript null and undefined
    java防止表单重复提交
    Java http post
    Redhat 6.5 x64 下载地址
    Spring 官方下载地址(非Maven)
  • 原文地址:https://www.cnblogs.com/windlog/p/12867449.html
Copyright © 2020-2023  润新知