110,538
社区成员
发帖
与我相关
我的任务
分享
private void button1_Click(object sender, EventArgs e)
{
byte[] b = System.Text.ASCIIEncoding.ASCII.GetBytes("00000111101010101010101101010101");
float a = BitConverter.ToSingle(b, 0);
textBox1.Text = a.ToString();
}
// 数据类型 浮点数与十六进制转化
public string FloatToHex(float floatValue)
{
uint uintValue = BitConverter.ToUInt32(BitConverter.GetBytes(floatValue), 0);
byte[] byteValue = BitConverter.GetBytes(uintValue);
Array.Reverse(byteValue);
return BitConverter.ToString(byteValue).Replace("-","");
}
public float HexToFloat(String hexString)
{
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
byte[] floatVals = BitConverter.GetBytes(num);
return BitConverter.ToSingle(floatVals, 0);
}
using System;
using System.Text;
class Program
{
static void Main()
{
float ff = 52.368F;
byte[] bb = BitConverter.GetBytes(ff);
string s = ToString(bb);
Console.WriteLine(s);
Console.WriteLine(ToSingle(s));
}
static string ToString(byte[] bb)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in bb)
{
sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
}
return sb.ToString();
}
static float ToSingle(string s)
{
byte[] b = new byte[4];
byte[] d = { 1, 2, 4, 8, 16, 32, 64, 128 };
for (int i = 0; i < 4; i++)
{
b[i] = 0;
for (int j = 0; j < 8; j++)
{
if (s[i*8 + (7-j)] == '1') b[i] += d[j];
}
}
return BitConverter.ToSingle(b, 0);
}
}
private void button1_Click(object sender, EventArgs e)
{
string s = "00000111101010101010101101010101";
byte[] b = new byte[4];
byte[] d = { 1, 2, 4, 8, 16, 32, 64, 128 };
for (int i = 0; i < 4; i++)
{
b[i] = 0;
for (int j = 0; j < 8; j++)
{
if (s[(3-i)*8 + (7-j)] == '1') b[i] += d[j];
}
}
float a = BitConverter.ToSingle(b, 0);
textBox1.Text = a.ToString();
}
float ff = 52.368F;
byte[] bb = BitConverter.GetBytes(ff);
float f = BitConverter.ToSingle(bb, 0);
this.textBox1.Text = f.ToString();