C# преобразование 4byte в 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); }
Что я уже пробовал:
Я нашел это, и да, это работает. Но я хотел бы знать, как я могу сделать упрощенную функцию, может ли кто-нибудь помочь?
<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. Спасибо
Richard MacCutchan
Тип C# float уже является IEE754.