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));
        }
  • 相关阅读:
    【转】如何高效地阅读技术类书籍与博客
    测试站点大全
    【转】软件测试面试- 购物车功能测试用例设计
    div+css 定位浅析
    C# Enum,Int,String的互相转换
    sqlserver 空间数据类型
    系统学习sqlserver2012 一
    sql查询数据库中所有表的记录条数,以及占用磁盘空间大小。
    linux网站推荐
    匿名用户访问sharepoint2010中的列表
  • 原文地址:https://www.cnblogs.com/windlog/p/12867449.html
Copyright © 2011-2022 走看看