C#将4byte转换为IEEE754 64位 [英] C# convert 4byte to IEEE754 64bits
问题描述
private void Form1_Load(object sender, EventArgs e)
{
// Initial Value
float num1 = 36;
// Convert to IEEE 754
uint num2 = BitConverter.ToUInt32(BitConverter.GetBytes(num1),0);
Console.WriteLine("{0} : {1}", num1, num2);
// Hex representation
byte[] byteArray = BitConverter.GetBytes(num1);
//Array.Reverse(byteArray);
Console.WriteLine(BitConverter.ToString(byteArray).Replace("-", " "));
double doubleValue = BitConverter.Int64BitsToDouble(num2);
Console.WriteLine(num2);
// Convert back to float
float num3 = BitConverter.ToSingle(BitConverter.GetBytes(num2), 0);
Console.WriteLine(num3);
}
我的尝试:
我发现了这个,是的。但我想知道我可以做任何人帮助的简化功能吗?
What I have tried:
I found this and yes that works. But I wanted to know how I can do a simplified function can anyone help?
<pre>using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication18
{
public partial class Form1 : Form
{
private ByteConverter _converter;
public Form1()
{
InitializeComponent();
}
private void convertU30()
{
if (int.Parse(textBox1.Text) < 1)
return;
string bin = (Convert.ToString(int.Parse(textBox1.Text), 2));
string swappedbin = "";
Console.WriteLine(bin);
while (bin.Length > 0)
{
if (bin.Length > 7)
{
swappedbin += "1" + bin.Substring(bin.Length - 7);
bin = bin.Substring(0, bin.Length - 7);
}
else
{
while (bin.Length < 8)
bin = "0" + bin;
swappedbin += bin;
bin = "";
}
}
textBox5.Text = Convert.ToInt32(swappedbin, 2).ToString("X");
}
private void Form1_Load(object sender, EventArgs e)
{
_converter = new ByteConverter();
textBox5.DataBindings.Add("Text", _converter, "U30", true, DataSourceUpdateMode.OnPropertyChanged, 0, "X");
tb_doubleHex.DataBindings.Add("Text", _converter, "ieee754x64", true, DataSourceUpdateMode.OnPropertyChanged, 0, "X");
}
private void button1_Click(object sender, EventArgs e)
{
if (Regex.IsMatch(textBox1.Text, @"^-*\d+$"))
{
convertU30();
}
else
{
textBox1.BackColor = Color.Red;
}
}
internal class ByteConverter : INotifyPropertyChanged
{
private int _4byte;
private int _u30;
private double _ieee754x64;
public event PropertyChangedEventHandler PropertyChanged;
public string U30
{
get
{
return this._u30.ToString("X");
}
set
{
this._u30 = this.HandleHexInput(value);
this._4byte = this.convertU30ToInt(this._u30);
this._ieee754x64 = Convert.ToDouble(this._4byte);
this.OnPropertyChanged(nameof(U30));
}
}
public string ieee754x64
{
get
{
byte[] bytes = BitConverter.GetBytes(_ieee754x64);
return string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}{5:X2}{6:X2}{7:X2}", (object)bytes[7], (object)bytes[6], (object)bytes[5], (object)bytes[4], (object)bytes[3], (object)bytes[2], (object)bytes[1], (object)bytes[0]);
}
set
{
_ieee754x64 = HandleIEEE754x64Input(value);
try
{
}
catch (Exception ex)
{
int num = (int)MessageBox.Show(ex.Message, "Critical Error");
}
}
}
private double HandleIEEE754x64Input(string val)
{
val = val.Replace(" ", string.Empty);
if (!Regex.IsMatch(val, "^[\\da-fA-F]+$"))
return 0.0;
try
{
return BitConverter.ToDouble(BitConverter.GetBytes(Convert.ToInt64(val, 16)), 0);
}
catch
{
return 0.0;
}
}
private int HandleHexInput(string val)
{
if (!Regex.IsMatch(val, "^[\\da-fA-F]+$"))
return 0;
try
{
return Convert.ToInt32(val, 16);
}
catch
{
return 0;
}
}
private int convertU30ToInt(int val)
{
try
{
string str1 = Convert.ToString(val, 2);
string str2 = string.Empty;
while (str1.Length > 0)
{
if (str1.Length > 8)
{
str2 = str1.Substring(1, 7) + str2;
str1 = str1.Substring(8);
}
else
{
while (str1.Length < 8)
str1 = "0" + str1;
str2 = str1 + str2;
str1 = "";
}
}
return Convert.ToInt32(str2, 2);
}
catch { }
{
return this._u30;
}
}
private void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if (propertyChanged == null)
return;
propertyChanged((object)this, new PropertyChangedEventArgs(PropertyName));
}
}
}
}
大家好,我需要你的帮助。需要将4个字节转换为ieee754。但是,示例格式编号36必须导致42 40十六进制。数字100是59 40.谢谢
Hello everyone, I need your help. need to convert 4 bytes to ieee754. however, it is necessary that the example format number 36 results in 42 40 in hex. and the number 100 is 59 40. Thanks
推荐答案
))
{
convertU30();
}
else
{
textBox1.BackColor = Color.Red;
}
}
内部类ByteConverter:INotifyPropertyChanged
{
private int _4byte ;
private int _u30;
private double _ieee754x64;
公共事件PropertyChangedEventHandler PropertyChanged;
public string U30
{
get
{
return this._u30.ToString(X);
}
set
{
this._u30 = this.HandleHexInput(value );
this._4byte = this.convertU3 0ToInt(this._u30);
this._ieee754x64 = Convert.ToDouble(this._4byte);
this.OnPropertyChanged(nameof(U30));
}
}
公共字符串ieee754x64
{
get
{
byte [] bytes = BitConverter.GetBytes(_ieee754x64 );
返回string.Format({0:X2} {1:X2} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} ,(对象)bytes [7],(object)bytes [6],(object)bytes [5],(object)bytes [4],(object)bytes [3],(object)bytes [2], (object)bytes [1],(object)bytes [0]);
}
设置
{
_ieee754x64 = HandleIEEE754x64Input(value);
try
{
}
catch(exception ex)
{
int num =(int)MessageBox.Show(ex.Message) , 关键错误);
}
}
}
private double HandleIEEE754x64Input(string val)
{
val = val.Replace( ,string.Empty);
if(!Regex.IsMatch(val,^ [\\da-fA-F] +
")) { convertU30(); } else { textBox1.BackColor = Color.Red; } } internal class ByteConverter : INotifyPropertyChanged { private int _4byte; private int _u30; private double _ieee754x64; public event PropertyChangedEventHandler PropertyChanged; public string U30 { get { return this._u30.ToString("X"); } set { this._u30 = this.HandleHexInput(value); this._4byte = this.convertU30ToInt(this._u30); this._ieee754x64 = Convert.ToDouble(this._4byte); this.OnPropertyChanged(nameof(U30)); } } public string ieee754x64 { get { byte[] bytes = BitConverter.GetBytes(_ieee754x64); return string.Format("{0:X2}{1:X2}{2:X2}{3:X2}{4:X2}{5:X2}{6:X2}{7:X2}", (object)bytes[7], (object)bytes[6], (object)bytes[5], (object)bytes[4], (object)bytes[3], (object)bytes[2], (object)bytes[1], (object)bytes[0]); } set { _ieee754x64 = HandleIEEE754x64Input(value); try { } catch (Exception ex) { int num = (int)MessageBox.Show(ex.Message, "Critical Error"); } } } private double HandleIEEE754x64Input(string val) { val = val.Replace(" ", string.Empty); if (!Regex.IsMatch(val, "^[\\da-fA-F]+
))
返回0.0;
try
{
返回BitConverter.ToDouble(BitConverter.GetBytes(Convert.ToInt64(val,16)),0);
}
catch
{
return 0.0;
}
}
private int HandleHexInput(string val)
{
if(!Regex.IsMatch(val,^ [\\ da-fA-F] +
")) return 0.0; try { return BitConverter.ToDouble(BitConverter.GetBytes(Convert.ToInt64(val, 16)), 0); } catch { return 0.0; } } private int HandleHexInput(string val) { if (!Regex.IsMatch(val, "^[\\da-fA-F]+
))
返回0;
try
{
return Convert.ToInt32(val,16);
}
catch
{
return 0;
}
}
private int convertU30ToInt(int val)
{
try
{
string str1 = Convert.ToString (val,2);
string str2 = string.Empty;
while(str1.Length> 0)
{
if(str1.Length> 8)
{
str2 = str1.Substring(1,7) + str2;
str1 = str1.Substring(8);
}
else
{
while(str1.Length< 8)
str1 =0+ str1;
str2 = str1 + str2;
str1 =;
}
}
返回Convert.ToInt32(str2,2);
}
catch {}
{
return this._u30;
}
}
private void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if(propertyChanged == null)
return;
propertyChanged((object)this,new PropertyChangedEventArgs(PropertyName));
}
}
}
}
")) return 0; try { return Convert.ToInt32(val, 16); } catch { return 0; } } private int convertU30ToInt(int val) { try { string str1 = Convert.ToString(val, 2); string str2 = string.Empty; while (str1.Length > 0) { if (str1.Length > 8) { str2 = str1.Substring(1, 7) + str2; str1 = str1.Substring(8); } else { while (str1.Length < 8) str1 = "0" + str1; str2 = str1 + str2; str1 = ""; } } return Convert.ToInt32(str2, 2); } catch { } { return this._u30; } } private void OnPropertyChanged(string PropertyName) { PropertyChangedEventHandler propertyChanged = this.PropertyChanged; if (propertyChanged == null) return; propertyChanged((object)this, new PropertyChangedEventArgs(PropertyName)); } } } }
大家好,我需要你的帮助。需要将4个字节转换为ieee754。但是,示例格式编号36必须导致42 40十六进制。数字100是59 40.谢谢
Hello everyone, I need your help. need to convert 4 bytes to ieee754. however, it is necessary that the example format number 36 results in 42 40 in hex. and the number 100 is 59 40. Thanks
这篇关于C#将4byte转换为IEEE754 64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!