zoukankan      html  css  js  c++  java
  • 多种数据类型与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;
    }



  • 相关阅读:
    搞懂树状数组
    C#接口(Interface)
    C#接口(Interface)
    C#运算符的重载
    C#和C++的区别(一)
    hdu1874 畅通工程续
    hdu1874 畅通工程续
    C#多态性
    C#多态性
    C#继承
  • 原文地址:https://www.cnblogs.com/graypigeon/p/2344893.html
Copyright © 2011-2022 走看看