在计算机的内存中,所有数据都是使用字节(byte)来存储的。不同的CPU对于字节的存储顺序可能是不一致的。
比如1000在某些CPU上存储顺序是0x03,0xE8。而有些CPU存储顺序是0xE8,0x03。
下面演示C#基础数据类型所占用的字节数,以及转化为相应的字节数组。
一、测试源程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
namespace BasicDataTypeDemp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"【bool】所占用的字节数【{sizeof(bool)}】");
Console.WriteLine($"【sbyte】所占用的字节数【{sizeof(sbyte)}】");
Console.WriteLine($"【byte】所占用的字节数【{sizeof(byte)}】");
Console.WriteLine($"【short】所占用的字节数【{sizeof(short)}】");
Console.WriteLine($"【ushort】所占用的字节数【{sizeof(ushort)}】");
Console.WriteLine($"【char】所占用的字节数【{sizeof(char)}】");
Console.WriteLine($"【int】所占用的字节数【{sizeof(uint)}】");
Console.WriteLine($"【uint】所占用的字节数【{sizeof(uint)}】");
Console.WriteLine($"【float】所占用的字节数【{sizeof(float)}】");
Console.WriteLine($"【long】所占用的字节数【{sizeof(long)}】");
Console.WriteLine($"【ulong】所占用的字节数【{sizeof(ulong)}】");
Console.WriteLine($"【double】所占用的字节数【{sizeof(double)}】");
Console.WriteLine($"【decimal】所占用的字节数【{sizeof(decimal)}】");
byte[] bufferBool = BitConverter.GetBytes(true);
Console.WriteLine($"【bool】类型转化为【1】个字节:{string.Join(",", bufferBool)}");
byte[] bufferShort = BitConverter.GetBytes((short)-20000);
Console.WriteLine($"【short】类型转化为【2】个字节:{string.Join(",", bufferShort)}");
byte[] bufferUshort = BitConverter.GetBytes((ushort)45536);
Console.WriteLine($"【ushort】类型转化为【2】个字节:{string.Join(",", bufferUshort)}");
byte[] bufferChar = BitConverter.GetBytes('中');
Console.WriteLine($"【char】类型转化为【2】个字节:{string.Join(",", bufferChar)}");
byte[] bufferInt = BitConverter.GetBytes(-1234567);
Console.WriteLine($"【int】类型转化为【4】个字节:{string.Join(",", bufferInt)}");
byte[] bufferUint = BitConverter.GetBytes((uint)1234567);
Console.WriteLine($"【uint】类型转化为【4】个字节:{string.Join(",", bufferUint)}");
byte[] bufferFloat = BitConverter.GetBytes(12345.3456F);
Console.WriteLine($"【float】类型转化为【4】个字节:{string.Join(",", bufferFloat)}");
byte[] bufferLong = BitConverter.GetBytes(-123456789876L);
Console.WriteLine($"【long】类型转化为【8】个字节:{string.Join(",", bufferLong)}");
byte[] bufferUlong = BitConverter.GetBytes((ulong)1234567898764321L);
Console.WriteLine($"【ulong】类型转化为【8】个字节:{string.Join(",", bufferUlong)}");
byte[] bufferDouble = BitConverter.GetBytes(12345678.40625);
Console.WriteLine($"【double】类型转化为【8】个字节:{string.Join(",", bufferDouble)}");
decimal dec = 123456789.123456789M;
int[] arrayFourInt = decimal.GetBits(dec);
Console.WriteLine($"【decimal】类型转化为【4】个整数【int】:{string.Join(",", arrayFourInt)}");
IEnumerable<byte> bufferDecimal = arrayFourInt.SelectMany(element => BitConverter.GetBytes(element));
Console.WriteLine($"【decimal】类型转化为【16】个字节:{string.Join(",", bufferDecimal)}");
Console.WriteLine($"本计算机是否是低字节在前:【{BitConverter.IsLittleEndian}】");
short test = 1000;
Console.WriteLine($"【{test}】使用格式化字符串是:{test.ToString("X4")}");
List<string> list = new List<string>();
BitConverter.GetBytes(test).ToList().ForEach(num => list.Add(num.ToString("X2")));
Console.WriteLine($"【{test}】使用字节转化类是:{string.Join("", list)}");
Console.WriteLine($"可以看出【普通格式化是高字节在前】,使用【BitConverter是低字节在前的】,字节顺序恰好相反");
Console.ReadLine();
}
}
}
二、程序运行如图: