static void Main(string[] args)
{
//Console.WriteLine( getbyteBitValue(4,3));
float f =12.032f;
//1.浮点数转字符数组
byte[] buffs = BitConverter.GetBytes(f);
Console.WriteLine(buffs.Length + "\r\n");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buffs.Length; i++)
{
sb.Append(buffs[i] + " ");
}
Console.WriteLine(sb.ToString());
//2.字符数组转成浮点数
float f1= BitConverter.ToSingle(buffs,0);
Console.WriteLine(f1);
//3.字节数组转ushort
ushort[] us = new ushort[2];
us[0] = BitConverter.ToUInt16(buffs,0);
us[1] = BitConverter.ToUInt16(buffs, 2);
Console.WriteLine(us[0]+" "+us[1]);
//4.将ushort转成浮点数
List<byte> res = new List<byte>();
res.AddRange(BitConverter.GetBytes(us[0]));
res.AddRange(BitConverter.GetBytes(us[1]));
Console.WriteLine( BitConverter.ToSingle(res.ToArray(),0));
//5.将字符数组转成2进制
byte b = 112;
Console.WriteLine( Convert.ToString(b,2));
//6.整形转字节数组及字节数组转整形
int m = 123456;
byte[] by= BitConverter.GetBytes(m);
Console.WriteLine(by.Length);
StringBuilder sb1 = new StringBuilder();
for (int i = 0; i < by.Length; i++)
{
sb1.Append(by[i]+" ");
}
Console.WriteLine(sb1.ToString());
Console.WriteLine( BitConverter.ToInt32(by,0));
Console.ReadKey();
}
//7.判定字节中某一位是否是1
private static bool getbyteBitValue(byte v1, int v2)
{
string s = Convert.ToString(v1, 2);
if (s.Length < v2 + 1) return false;
else
{
if (s[s.Length-1-v2] == '1')
{
return true;
}
else
{
return false;
}
}
}
字节及其数组的相互转换
于 2022-12-21 11:28:53 首次发布