• C# 常用数据类型转换


    1.字符串类型浮点数转float类型

    1. string strNumber = "71.068";
    2. float floatNumber =float.Parse(strNumber); //输出floatNumber =71.068

    2.浮点数转4个字节(float类型),double默认转8个字节

    1. float floatNumber = 71.068f;
    2. byte[] result = BitConverter.GetBytes(floatNumber);//float浮点数默认转为4个字节,double默认转8个字节
    3. //输出结果:D1 22 8E 42
    4. Array.Reverse(result); //数组元素翻转 42 8E 22 D1

    3.int转4个字节 ,short默认转2个字节

    1. byte[] result2 = BitConverter.GetBytes((int)23.5); //将23.5转为int即23
    2. Array.Reverse(result2); //输出结果 00 00 00 17

    4. list集合转数组(一维)

    1. List<byte> listBytes1 = new List<byte>();
    2. listBytes1.Add(0x01);
    3. listBytes1.Add(0x02);
    4. listBytes1.Add(0x03);
    5. byte[] result3 = listBytes1.ToArray(); //result3存放的为0x01,0x02,0x03

    5.list集合转数组(二维)

    1. List<byte[]> listBytes1 = new List<byte[]>();
    2. float floatNumber = 71.068f;
    3. byte[] result = BitConverter.GetBytes(floatNumber);//float浮点数默认转为4个字节,double默认转8个字节
    4. //输出结果:D1 22 8E 42
    5. Array.Reverse(result); //数组元素翻转 42 8E 22 D1
    6. listBytes1.Add(result); //添加到集合中去
    7. byte[] result2 = BitConverter.GetBytes((int)23.5);
    8. Array.Reverse(result2); //输出结果 00 00 00 17
    9. listBytes1.Add(result2); //添加到集合中去
    10. byte[] result3 = listBytes1.SelectMany(bytes => bytes).ToArray(); //集合转数组
    11. //输出结果:42 8E 22 D1 00 00 00 17

  • 相关阅读:
    树莓派Linux内核编译
    React Native V0.74 — 稳定版已发布
    10.5作业
    Linux 中使普通用户使用Sudo不需要输入密码
    React 钩子汇总
    C++ atomic 和 memory ordering
    【项目经理】目标管理工具
    Rust结构体和枚举
    【Linux】源码编译安装openssl
    Mac安装win程序另一个方案
  • 原文地址:https://blog.csdn.net/m0_47020592/article/details/134425147