IEEE754规定如下的计算方法,可是我老是写不对,请各位高手帮忙计算公式:
V=(-1)^s*2^E*M当e(各位)为全'0'时,E=1-(2^(e(位数)-1)-1),;M=m。
如:real*4是8位,E=1-(2^(8-1)-1)=1-127=-126
即,
在real*4时:
V=(-1)^s*2^(-126)*m
在real*8时:
V=(-1)^s*2^(-1022)*m当e(各位)不为全'0'且不为全'1'时,E=e(值)-(2^(e(位数)-1)-1);M=1+m。
即,
在real*4时:
V=(-1)^s*2^(e(值)-127)*(1+m)
在real*8时:
V=(-1)^s*2^(e(值)-1023)*(1+m)
三:将浮点格式转换成十进制数[例3.1]:
0x00280000(real*4)
转换成二进制
00000000001010000000000000000000
符号位 指数部分(8位) 尾数部分
0 00000000 01010000000000000000000
符号位=0;因指数部分=0,则:尾数部分M为m:
0.01010000000000000000000=0.3125
该浮点数的十进制为:
(-1)^0*2^(-126)*0.3125
=3.6734198463196484624023016788195e-39
[例3.2]:
0xC04E000000000000(real*8)
转换成二进制
1100000001001110000000000000000000000000000000000000000000000000
符号位 指数部分(11位) 尾数部分
1 10000000100 1110000000000000000000000000000000000000000000000000
符号位=1;指数=1028,因指数部分不为全'0'且不为全'1',则:尾数部分M为1+m:
1.1110000000000000000000000000000000000000000000000000=1.875
该浮点数的十进制为:
(-1)^1*2^(1028-1023)*1.875
=-60
四:将十进制数转换成浮点格式(real*4)[例4.1]:
26.0
十进制26.0转换成二进制
11010.0
规格化二进制数
1.10100*2^4
计算指数
4+127=131
符号位 指数部分 尾数部分
0 10000011 10100000000000000000000
以单精度(real*4)浮点格式存储该数
0100 0001 1101 0000 0000 0000 0000 0000
0x41D0 0000[例4.2]:
0.75
十进制0.75转换成二进制
0.11
规格化二进制数
1.1*2^-1
计算指数
-1+127=126
符号位 指数部分 尾数部分
0 01111110 10000000000000000000000
以单精度(real*4)浮点格式存储该数
0011 1111 0100 0000 0000 0000 0000 0000
0x3F40 0000[例4.3]:
-2.5
十进制-2.5转换成二进制
-10.1
规格化二进制数
-1.01*2^1
计算指数
1+127=128
符号位 指数部分 尾数部分
1 10000000 01000000000000000000000
以单精度(real*4)浮点格式存储该数
1100 0000 0010 0000 0000 0000 0000 0000
0xC020 0000

解决方案 »

  1.   

    恰好写过// 对于大小为32-bit的浮点数(32-bit为单精度,64-bit浮点数为双精度,80-bit为扩展精度浮点数), 
    // 1、其第31 bit为符号位,为0则表示正数,反之为复数,其读数值用s表示; 
    // 2、第30~23 bit为幂数,其读数值用e表示; 
    // 3、第22~0 bit共23 bit作为系数,视为二进制纯小数,假定该小数的十进制值为x; 
    //
    // 则按照规定,该浮点数的值用十进制表示为: 
    // = (-1)^s  * (1 + x) * 2^(e - 127) 
    //
    // 对于49E48E68来说, 
    // 1、其第31 bit为0,即s = 0 
    // 2、第30~23 bit依次为100 1001 1,读成十进制就是147,即e = 147。 
    // 3、第22~0 bit依次为110 0100 1000 1110 0110 1000,也就是二进制的纯小数0.110 0100 1000 1110 0110 1000,其十进制形式为0.78559589385986328125,即x = 0.78559589385986328125。 
    //
    // 这样,该浮点数的十进制表示 
    //    = (-1)^s  * (1 + x) * 2^(e - 127) 
    // = (-1)^0  * (1+ 0.78559589385986328125) * 2^(147-127) 
    // = 1872333 
    //
    // 你可以用windows自带的计算器算一下。 
    public static string HexToFloat( string strHex )
    {
    string strBase16 = strHex ;
    string strTemp = "" ;
    double temp = 0 ;
    int m_s = 0 ; // 数符
    int m_e = 0 ; // 阶
    double m_x = 0 ; // 小数部分
    double m_re = 0 ; // 计算结果 strTemp = strBase16.Substring( 0, 2 ) ;
    temp = Convert.ToInt32( strTemp, 16 ) & 0x80 ;
    if( temp == 128 ) m_s = 1 ; strTemp = strBase16.Substring( 0, 3 ) ;
    temp = Convert.ToInt32( strTemp, 16 ) & 0x7f8 ;
    m_e = Convert.ToInt32( temp / Math.Pow( 2, 3 ) ) ; strTemp = strBase16.Substring( 2, 6 ) ;
    temp = Convert.ToInt32( strTemp, 16 ) & 0x7fffff ;
    m_x = temp / Math.Pow( 2, 23 ) ; m_re = Math.Pow( -1, m_s ) * ( 1 + m_x ) * Math.Pow( 2, m_e - 127 ) ; return decimal.Round( Convert.ToDecimal( m_re ), 5 ).ToString() ;
    }
      

  2.   

    public static double IEEE754Float(byte [] data, int i)
    {
    int s;
    int e;
    double m;
    double m_dblReturn = 0; s = data[i + 2] & 128;
    e = (data[i + 2] & 127) * 2 + (data[i + 3] & 128) / 128;
    m = (Convert.ToDouble((data[i + 3] & 127)) * 65536 + Convert.ToDouble(data[i]) * 256 + Convert.ToDouble(data[i + 1])) / 8388608;
    m_dblReturn = Math.Pow((-1),s) * Math.Pow(2 , (e - 127)) * (m + 1); return m_dblReturn;}public static double IEEE754Float(string p_strValue)
    {
    int s;
    int e;
    double m;
    double m_dblReturn = 0; byte [] data = new byte[4]; data[0] = Convert.ToByte(p_strValue.Substring(0,2),16);
    data[1] = Convert.ToByte(p_strValue.Substring(2,2),16);
    data[2] = Convert.ToByte(p_strValue.Substring(4,2),16);
    data[3] = Convert.ToByte(p_strValue.Substring(6,2),16); s = data[2] & 128;
    e = (data[2] & 127) * 2 + (data[3] & 128) / 128;
    m = (Convert.ToDouble((data[3] & 127)) * 65536 + Convert.ToDouble(data[0]) * 256 + Convert.ToDouble(data[1])) / 8388608;
    m_dblReturn = Math.Pow((-1),s) * Math.Pow(2 , (e - 127)) * (m + 1); return m_dblReturn;}反着转没有写过
    sorry
      

  3.   

    HEX--〉float好了float-->HEX如何做啊?
      

  4.   

    大概是先取
    1.
    得到数符
    Math.Pow((-1),s)
    2.
    计算阶
    循环除2,余数在1~2之间
    3.
    步骤2中的1~2之间的数-1得到小数部分
    4.
    转化为“0011”样式或者bytes[]
    5.
    然后
    Convert.ToString ( bytes[0+1] , 16 ) ;
    或者
    string bin = "1110" ;
    int dec=  Convert.ToInt16( bin , 2 ) ;
    string strBase16 = Convert.ToString(dec,16);
      

  5.   

    to  happyjun2000(蓝色游侠∮是非成败转头空,以后不再up了) 按HexToFloat函数调用,例如000045FA的浮点数应该是8000.00,但是这个函数返回的是0
      

  6.   

    查找指针,没想到找到了转换程序
    using System; 
    using System.Runtime.InteropServices; 
     
    namespace CSPointer 

        /**//// <summary> 
        /// PointerConvert 的摘要说明。 
        /// 指针转换类 
        /// 通过指针的方式更改数据类型 
        /// 支持:byte <-> int/float/double 
        ///      string 类型可以通过 
        ///      System.Text.Encoding进行编码 
        /// 用途:数据传输 
        /// 
        /// 作者:萧寒 
        /// http://www.cnblogs.com/chinasf 
        /// [email protected] 
        /// 最后更新日期:2005.5.27 
        /// </summary> 
        public unsafe class PointerConvert 
        { 
            public PointerConvert(){;} 
     
            /**//// <summary> 
            /// 转换Int数据到数组 
            /// </summary> 
            /// <param name="data"></param> 
            /// <returns></returns> 
            public static byte[] ToByte(int data) 
            { 
                unsafe  
                { 
                    byte* pdata = (byte*)&data; 
                    byte[] byteArray = new byte[sizeof(int)]; 
                    for (int i = 0; i < sizeof(int); ++i) 
                        byteArray[i] = *pdata++; 
                    return byteArray; 
                } 
            } 
             
     
            /**//// <summary> 
            /// 转换float数据到数组 
            /// </summary> 
            /// <param name="data"></param> 
            /// <returns></returns> 
            public static byte[] ToByte(float data) 
            { 
                unsafe  
                { 
                    byte* pdata = (byte*)&data; 
                    byte[] byteArray = new byte[sizeof(float)]; 
                    for (int i = 0; i < sizeof(float); ++i) 
                        byteArray[i] = *pdata++; 
                    return byteArray; 
                } 
            } 
     
            /**//// <summary> 
            /// 转换double数据到数组 
            /// </summary> 
            /// <param name="data"></param> 
            /// <returns></returns> 
            public static byte[] ToByte(double data) 
            { 
                unsafe  
                { 
                    byte* pdata = (byte*)&data; 
                    byte[] byteArray = new byte[sizeof(double)]; 
                    for (int i = 0; i < sizeof(double); ++i) 
                        byteArray[i] = *pdata++; 
                    return byteArray; 
                } 
            } 
     
     
            /**//// <summary> 
            /// 转换数组为整形 
            /// </summary> 
            /// <param name="data"></param> 
            /// <returns></returns> 
            public static int ToInt(byte[] data) 
            { 
                unsafe 
                { 
                    int n = 0; 
                    fixed(byte* p=data) 
                    { 
                        n = Marshal.ReadInt32((IntPtr)p); 
                    } 
                    return n; 
                } 
            } 
     
            /**//// <summary> 
            /// 转换数组为float 
            /// </summary> 
            /// <param name="data"></param> 
            /// <returns></returns> 
            public static float ToFloat(byte[] data) 
            { 
                float a=0; 
                byte i; 
                 
                byte[] x = data; 
                void *pf; 
                fixed(byte* px=x) 
                { 
                    pf =&a; 
                    for(i=0;i<data.Length;i++) 
                    { 
                        *((byte *)pf+i)=*(px+i); 
                    } 
                } 
     
                return a;             
            } 
         
            /**//// <summary> 
            /// 转换数组为Double 
            /// </summary> 
            /// <param name="data"></param> 
            /// <returns></returns> 
            public static double ToDouble(byte[] data) 
            { 
                double a=0; 
                byte i; 
                 
                byte[] x = data; 
                void *pf; 
                fixed(byte* px=x) 
                { 
                    pf =&a; 
                    for(i=0;i<data.Length;i++) 
                    { 
                        *((byte *)pf+i)=*(px+i); 
                    } 
                } 
                return a; 
            } 
     
     
        } 
    } http://www.cnblogs.com/chinasf/archive/2005/05/28/163932.html
      

  7.   

    按HexToFloat函数调用,例如000045FA的浮点数应该是8000.00,但是这个函数返回的是0re:
    8000.00应该是45FA0000也写了个反向解析public static string IEEE754Float( double floatValue )
    {
    Console.WriteLine( "================开始=============" ) ; Int64 Base16 = 0x00000000 ;
    int m_s = 0 ; // 数符
    int m_e = 0 ; // 阶
    double m_x = 0 ; // 小数部分
    // double m_re = 0 ; // 计算结果
    double absFloatValue = Math.Abs( floatValue ) ; if( floatValue < 0)
    {
    m_s = 1 ; // m_s = 1
    }
    // 共 31~0 位
    Base16 += Convert.ToInt64(  m_s * Math.Pow( 2, 31 ) ) ; // 首位,左移31位,到31位处 m_e = (int) Math.Log( absFloatValue, 2 ) + 127 ;
    Base16 +=  Convert.ToInt64( m_e * Math.Pow( 2, 23 ) ) ; // 首位,左移23个,到31~23位处 m_x = absFloatValue / Math.Pow( 2, m_e - 127 )  - 1 ;
    m_x = m_x * Math.Pow( 2, 23 ) ; //首位,左移22个,从-1位开始的段,到22~0位处
    Base16 += Convert.ToInt64( m_x ) ; Console.WriteLine( "================结束=============" ) ;

    return Base16.ToString( "X8" ) ;
    }
      

  8.   

    45FA0000
    000045FA
    我们高低为可能处理的不一样。
      

  9.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace CSPointer
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox tbLog;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button btnIntToByte;
    private System.Windows.Forms.CheckBox isShowHexData;
    private System.Windows.Forms.NumericUpDown nUD_int;
    private System.Windows.Forms.Button btnClearLog;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button btnFloatToByte;
    private System.Windows.Forms.NumericUpDown nUD_float;
    private System.Windows.Forms.Button btnDoubleToByte;
    private System.Windows.Forms.NumericUpDown nUD_Double;
    private System.Windows.Forms.Label label3;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.tbLog = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.nUD_int = new System.Windows.Forms.NumericUpDown();
    this.btnIntToByte = new System.Windows.Forms.Button();
    this.isShowHexData = new System.Windows.Forms.CheckBox();
    this.btnClearLog = new System.Windows.Forms.Button();
    this.btnFloatToByte = new System.Windows.Forms.Button();
    this.nUD_float = new System.Windows.Forms.NumericUpDown();
    this.label2 = new System.Windows.Forms.Label();
    this.btnDoubleToByte = new System.Windows.Forms.Button();
    this.nUD_Double = new System.Windows.Forms.NumericUpDown();
    this.label3 = new System.Windows.Forms.Label();
    ((System.ComponentModel.ISupportInitialize)(this.nUD_int)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.nUD_float)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.nUD_Double)).BeginInit();
    this.SuspendLayout();
    // 
    // tbLog
    // 
    this.tbLog.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Right)));
    this.tbLog.Location = new System.Drawing.Point(232, 24);
    this.tbLog.Multiline = true;
    this.tbLog.Name = "tbLog";
    this.tbLog.ReadOnly = true;
    this.tbLog.ScrollBars = System.Windows.Forms.ScrollBars.Both;
    this.tbLog.Size = new System.Drawing.Size(328, 488);
    this.tbLog.TabIndex = 0;
    this.tbLog.Text = "";
    // 
    // label1
    // 
    this.label1.AutoSize = true;
    this.label1.Location = new System.Drawing.Point(8, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 17);
    this.label1.TabIndex = 1;
    this.label1.Text = "整形转数组:";
    // 
    // nUD_int
    // 
    this.nUD_int.Location = new System.Drawing.Point(8, 32);
    this.nUD_int.Maximum = new System.Decimal(new int[] {
    276447231,
    23283,
    0,
    0});
    this.nUD_int.Name = "nUD_int";
    this.nUD_int.TabIndex = 2;
    this.nUD_int.Value = new System.Decimal(new int[] {
      123456789,
      0,
      0,
      0});
    // 
    // btnIntToByte
    // 
    this.btnIntToByte.Location = new System.Drawing.Point(136, 32);
    this.btnIntToByte.Name = "btnIntToByte";
    this.btnIntToByte.TabIndex = 3;
    this.btnIntToByte.Text = "测试";
    this.btnIntToByte.Click += new System.EventHandler(this.btnIntToByte_Click);
    // 
    // isShowHexData
    // 
    this.isShowHexData.Checked = true;
    this.isShowHexData.CheckState = System.Windows.Forms.CheckState.Checked;
    this.isShowHexData.Location = new System.Drawing.Point(232, 0);
    this.isShowHexData.Name = "isShowHexData";
    this.isShowHexData.Size = new System.Drawing.Size(112, 24);
    this.isShowHexData.TabIndex = 4;
    this.isShowHexData.Text = "16进制数据格式";
    // 
    // btnClearLog
    // 
    this.btnClearLog.Location = new System.Drawing.Point(352, 0);
    this.btnClearLog.Name = "btnClearLog";
    this.btnClearLog.TabIndex = 5;
    this.btnClearLog.Text = "清空输出";
    this.btnClearLog.Click += new System.EventHandler(this.btnClearLog_Click);
    // 
    // btnFloatToByte
    // 
    this.btnFloatToByte.Location = new System.Drawing.Point(136, 88);
    this.btnFloatToByte.Name = "btnFloatToByte";
    this.btnFloatToByte.TabIndex = 8;
    this.btnFloatToByte.Text = "测试";
    this.btnFloatToByte.Click += new System.EventHandler(this.btnFloatToByte_Click);
    // 
    // nUD_float
    // 
    this.nUD_float.DecimalPlaces = 6;
    this.nUD_float.Location = new System.Drawing.Point(8, 88);
    this.nUD_float.Maximum = new System.Decimal(new int[] {
      276447231,
      23283,
      0,
      0});
    this.nUD_float.Name = "nUD_float";
    this.nUD_float.TabIndex = 7;
    this.nUD_float.Value = new System.Decimal(new int[] {
    123456789,
    0,
    0,
    0});
      

  10.   


    // 
    // label2
    // 
    this.label2.AutoSize = true;
    this.label2.Location = new System.Drawing.Point(8, 64);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(72, 17);
    this.label2.TabIndex = 6;
    this.label2.Text = "浮点转数组:";
    // 
    // btnDoubleToByte
    // 
    this.btnDoubleToByte.Location = new System.Drawing.Point(136, 144);
    this.btnDoubleToByte.Name = "btnDoubleToByte";
    this.btnDoubleToByte.TabIndex = 11;
    this.btnDoubleToByte.Text = "测试";
    this.btnDoubleToByte.Click += new System.EventHandler(this.btnDoubleToByte_Click);
    // 
    // nUD_Double
    // 
    this.nUD_Double.DecimalPlaces = 6;
    this.nUD_Double.Location = new System.Drawing.Point(8, 144);
    this.nUD_Double.Maximum = new System.Decimal(new int[] {
       276447231,
       23283,
       0,
       0});
    this.nUD_Double.Name = "nUD_Double";
    this.nUD_Double.TabIndex = 10;
    this.nUD_Double.Value = new System.Decimal(new int[] {
     123456789,
     0,
     0,
     0});
    // 
    // label3
    // 
    this.label3.AutoSize = true;
    this.label3.Location = new System.Drawing.Point(8, 120);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(85, 17);
    this.label3.TabIndex = 9;
    this.label3.Text = "双精度转数组:";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(568, 517);
    this.Controls.Add(this.btnDoubleToByte);
    this.Controls.Add(this.nUD_Double);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.btnFloatToByte);
    this.Controls.Add(this.nUD_float);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.btnClearLog);
    this.Controls.Add(this.isShowHexData);
    this.Controls.Add(this.btnIntToByte);
    this.Controls.Add(this.nUD_int);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.tbLog);
    this.Name = "Form1";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "数据通过指针转换测试";
    ((System.ComponentModel.ISupportInitialize)(this.nUD_int)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.nUD_float)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.nUD_Double)).EndInit();
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void LogWrite(string s)
    {
    tbLog.Text += s;
    } private void LogWriteLn(string s)
    {
    LogWrite(s + "\r\n");
    } private void LogWriteLn()
    {
    LogWrite("\r\n");
    } private void btnClearLog_Click(object sender, System.EventArgs e)
    {
    tbLog.Clear();
    } private void btnIntToByte_Click(object sender, System.EventArgs e)
    {
    //将Int32转化为字节数组
    byte[] intByte = PointerConvert.ToByte(Convert.ToInt32(nUD_int.Value));
    //显示数据内容
    foreach(byte b in intByte)
    {
    LogWrite(String.Format((this.isShowHexData.Checked)?"{0:X2} ":"{0} ",b));
    }
    LogWriteLn();
    //将转化的字节数组回复为Int32
    int n = PointerConvert.ToInt(intByte);
    //显示数据内容
    LogWriteLn(n.ToString());
    } private void btnFloatToByte_Click(object sender, System.EventArgs e)
    {
    //将Single转化为字节数组
    byte[] floatByte = PointerConvert.ToByte(Convert.ToSingle(nUD_float.Value));
    //显示数据内容
    foreach(byte b in floatByte)
    {
    LogWrite(String.Format((this.isShowHexData.Checked)?"{0:X2} ":"{0} ",b));
    }
    LogWriteLn();
    //将转化的字节数组回复为Single
    float n = PointerConvert.ToFloat(floatByte);
    //显示数据内容
    LogWriteLn(n.ToString());
    } private void btnDoubleToByte_Click(object sender, System.EventArgs e)
    {
    //将Double转化为字节数组
    byte[] doubleByte = PointerConvert.ToByte(Convert.ToDouble(nUD_Double.Value));
    //显示数据内容
    foreach(byte b in doubleByte)
    {
    LogWrite(String.Format((this.isShowHexData.Checked)?"{0:X2} ":"{0} ",b));
    }
    LogWriteLn();
    //将转化的字节数组回复为Double
    Double n = PointerConvert.ToDouble(doubleByte);
    //显示数据内容
    LogWriteLn(n.ToString());
    }
    }
    }
      

  11.   

    using System;
    using System.Runtime.InteropServices;namespace CSPointer
    {
    /// <summary>
    /// PointerConvert 的摘要说明。
    /// 指针转换类
    /// 通过指针的方式更改数据类型
    /// 支持:byte <-> int/float/double
    ///      string 类型可以通过
    ///      System.Text.Encoding进行编码
    /// 用途:数据传输
    ///
    /// 作者:随飞
    /// http://www.cnblogs.com/chinasf
    /// [email protected]
    /// 最后更新日期:2005.5.27
    /// </summary>
    public unsafe class PointerConvert
    {
    public PointerConvert(){;} /// <summary>
    /// 转换Int数据到数组
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] ToByte(int data)
    {
    unsafe 
    {
    byte* pdata = (byte*)&data;
    byte[] byteArray = new byte[sizeof(int)];
    for (int i = 0; i < sizeof(int); ++i)
    byteArray[i] = *pdata++;
    return byteArray;
    }
    }
    /// <summary>
    /// 转换float数据到数组
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] ToByte(float data)
    {
    unsafe 
    {
    byte* pdata = (byte*)&data;
    byte[] byteArray = new byte[sizeof(float)];
    for (int i = 0; i < sizeof(float); ++i)
    byteArray[i] = *pdata++;
    return byteArray;
    }
    } /// <summary>
    /// 转换double数据到数组
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static byte[] ToByte(double data)
    {
    unsafe 
    {
    byte* pdata = (byte*)&data;
    byte[] byteArray = new byte[sizeof(double)];
    for (int i = 0; i < sizeof(double); ++i)
    byteArray[i] = *pdata++;
    return byteArray;
    }
    }
    /// <summary>
    /// 转换数组为整形
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static int ToInt(byte[] data)
    {
    unsafe
    {
    int n = 0;
    fixed(byte* p=data)
    {
    n = Marshal.ReadInt32((IntPtr)p);
    }
    return n;
    }
    } /// <summary>
    /// 转换数组为float
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static float ToFloat(byte[] data)
    {
    float a=0;
    byte i;

    byte[] x = data;
    void *pf;
    fixed(byte* px=x)
    {
    pf =&a;
    for(i=0;i<data.Length;i++)
    {
    *((byte *)pf+i)=*(px+i);
    }
    } return a;
    }

    /// <summary>
    /// 转换数组为Double
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static double ToDouble(byte[] data)
    {
    double a=0;
    byte i;

    byte[] x = data;
    void *pf;
    fixed(byte* px=x)
    {
    pf =&a;
    for(i=0;i<data.Length;i++)
    {
    *((byte *)pf+i)=*(px+i);
    }
    }
    return a;
    }
    }
    }
      

  12.   

    cnming(cnming) 的方法不错应该同
    BitConverter.ToSingle()
    等方法。白写了,呵呵~~
      

  13.   

    谢谢cnming(cnming) 和 happyjun2000(蓝色游侠∮是非成败转头空,以后不再up了) 问题解决了
      

  14.   

    // 正向
    float floatValue = 8000 ;
    byte[] bytes = BitConverter.GetBytes( floatValue ) ;
    int intValue = 0 ;
    intValue = BitConverter.ToInt32( bytes, 0 ) ;
    Console.WriteLine( intValue.ToString( "X8" ) ) ; // 逆向
    floatValue =  BitConverter.ToSingle( bytes, 0 ) ;
    Console.WriteLine( floatValue.ToString() ) ;
      

  15.   

    原来BitConverter提供了方法,大家都白忙了,呵呵~~
      

  16.   

    谢谢 happyjun2000(蓝色游侠∮是非成败转头空,以后不再up了) 我忙乎了两天了
      

  17.   

    也想过是否C#就具备转换的,但是由于我的笔记本报修,在我哥哥的笔记本上由于没有空间了,只安装了C#和VC,没有MSDN痛苦之中