複数のデータ型とbyte[]バイト配列の変換レコード
4510 ワード
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;
}