• 多种数据类型与byte[]字节数组的转换记录


     1 //把其它数字类型:int(32)/long(64)/ushort(16)存入byte[],根据后面的位数偏移,后一位 + 8
    2 int a = 0x911245;
    3 byte[] bys = new byte[4];
    4 bys[0] = (byte)(a);
    5 bys[1] = (byte)(a >> 8);
    6 bys[2] = (byte)(a >> 16);
    7 bys[3] = (byte)(a >> 24);
    8 for (int i = 0; i < bys.Length; i++)
    9 {
    10 Console.WriteLine(bys[i]);
    11 }
    //BitConverter转换
    public static void Main(string[] args)
    {
    int a = 0x911245;
    byte[] bys = BitConverter.GetBytes(a);

    //输出
    for (int i = 0; i < bys.Length; i++)
    {
    Console.WriteLine(bys[i]);
    }
    Console.WriteLine(a.ToString());
    Console.ReadKey();
    }
    //int >> byte[]
    public static byte[] GetBytesInt32(int argument)
    {
    byte[] byteArray = BitConverter.GetBytes(argument);
    return byteArray;
    }
    //byte >> char
    public static char BAToChar(byte[] bytes, int index)
    {
    char value = BitConverter.ToChar(bytes, index);
    return value;
    }
    //byte >> string
    public static string BaToString(byte[] bytes)
    {
    string str = BitConverter.ToString(bytes);
    return str;
    }



  • 相关阅读:
    shell 编写简单的整数计算器
    信号控制
    MySQL-索引及优化整理
    Java面试-Java容器有哪些
    C语言宏定义
    值类型与引用类型的区别
    C++虚函数简介
    DNS-域名解析
    扇区,簇,块区分
    Java合并两个数组为一个新数组
  • 原文地址:https://www.cnblogs.com/graypigeon/p/2344893.html
Copyright © 2020-2023  润新知