VC++好久都没有用了,电脑上也没有装它。现在由于要调试单片机,而单片机用的是32位浮点数,与通常的习惯不一样,所以想到让大家帮忙编个程序。第一个回答问题的人将得到绝大多数分。程序要求:IEEE的32位浮点数转16进制及十进制。(32位浮点数格式请参见http://home.eeworld.com.cn/my/space.php?uid=59045&do=blog&id=65045)

解决方案 »

  1.   

    注:32位浮点数的表示法用0xabcdef12,即16进制显示,及0101110000000……二进制显示
      

  2.   

    只要这段数据在内存中,用:WideCharToMultiByte()就可转换显示呀
      

  3.   

    示例 char sText[256];//转换后的汉字
    wchar_t wText[] = {L"我今年30岁了!"};
    WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,sText,sizeof(sText),NULL,FALSE);
      

  4.   

    看你的意思是类型装换吧?用整型惯了,浮点不适应?
    搞单片机汇编什么的兄弟,看设么数据都是16进->2进制的。你的数据 float f = 3.14f; //16进是40 48 F5 C3
    转换成4字节整型 int n = (int)(f*100+0.5);
    这时 n 就是整型 314       //16进是 00 00 01 3A你是这个意思?
      

  5.   

    在内存里3.14就已经是16进的40 48 F5 C3了,还转换啥呢?
    你是想把它印出来?下面代码
    union {
    float pi;
    int i4;
    }u;
    u.pi = 3.14;
    char str[64];
    sprintf(str,"%x",u.i4);
    这时这个串str里面就是字符串 4048F5C3,是这个意思?
      

  6.   

    回楼上。
    可能我没有说清楚吧,我是需要个程序,在windows XP中运行的程序,输入十进制数后,就可以显示IEEE的浮点数,或者输入以0x1234ABCD格式表示的IEEE浮点数,显示十进制或者16进制形式。例如,输入16进制数“0x3E8”,就可以显示十进制“1000”,IEEE浮数:“447A0000”
      

  7.   

    // I3e.cpp : Defines the entry point for the console application.
    //
    // Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format.
    // Single-precision values with float type have 4 bytes, consisting of a sign bit,
    // an 8-bit excess-127 binary exponent, and a 23-bit mantissa.
    // The mantissa represents a number between 1.0 and 2.0.
    // Since the high-order bit of the mantissa is always 1, 
    // it is not stored in the number.
    // You can declare variables as float or double, depending on the needs of your 
    // application. The principal differences between the two types are 
    // the significance they can represent, the storage they require, and their range.
    // Table 3.3 shows the relationship between significance and storage requirements.// Table 3.3:
    // Type Significant digits,Number of bytes 
    // float 6 –7 4 
    // double 15–16 8 // Floating-point variables are represented by a mantissa,
    // which contains the value of the number, and an exponent, 
    // which contains the order of magnitude of the number.
    // Table 3.4 shows the number of bits allocated to the mantissa 
    // and the exponent for each floating-point type. The most significant bit 
    // of any float or double is always the sign bit. If it is 1, the number is 
    // considered negative; otherwise, it is considered a positive number.
    // Table 3.4 :
    // Type Exponent length,Mantissa length 
    // float 8  bits 23 bits 
    // double 11 bits 52 bits // Because exponents are stored in an unsigned form,
    // the exponent is biased by half its possible value. For type float, 
    // the bias is 127; for type double, it is 1023. You can compute the 
    // actual exponent value by subtracting the bias value from the exponent value.
    // The mantissa is stored as a binary fraction greater than or equal to 1 
    // and less than 2. For types float and double, there is an implied leading 1 
    // in the mantissa in the most-significant bit position, so the mantissas are
    // actually 24 and 53 bits long, respectively, even though the most-significant bit
    // is never stored in memory.// Intel     IEEE 格式:   S(1bit) E(8bit) M(23bit)
    // Microsoft MBF  格式:   E(8bit) S(1bit) M(23bit)#include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //
    union IEEE
    {
    float f;// 微软浮点格式.与 INTEL 浮点格式(IEEE)不同
    int i;
    unsigned int dw;// 2 words
    } ;
    //
    void MBF(IEEE f)
    {
    IEEE nf;
    char bit[80];
    if(f.dw != 0)
    {
    unsigned int sign     = f.dw & 0x80000000;
    unsigned int  exp     = f.dw & 0x7F800000;
    unsigned int  mantissa= f.dw & 0x007FFFFF;
    nf.dw=0;
    nf.dw  = exp  << 1;
    nf.dw |= sign >> 8;
    nf.dw |= mantissa;
    itoa(nf.i,bit,2);
    int len=strlen(bit);
    if(len !=32)
    {
    strrev(bit);
    for(int jj=0;jj<(32-len);jj++)
    {
    strcat(bit,"0");
    }
    strrev(bit);
    }
    printf("MBF: %f = %32s = %08X\n",f.f,bit,nf.i);
    }
    }
    //
    int main(int argc, char* argv[])
    {
    IEEE xxx;
    char bit[80];
    // int  *P;
    // P=(int*)0x100;
    for (int jj=0;jj<10;jj++)
    {
    xxx.f=(float)jj/8;// change this for other data
    itoa(xxx.i,bit,2);//整数转换为二进制字串
    strrev(bit);// 翻转字串
    unsigned int len=strlen(bit);
    for (int jj=0;jj<(int)(32-len);jj++)
    {// 补零
    strcat(bit,"0");
    }
    strrev(bit);// 恢复
    printf("%f = %32s = %08X\n",xxx.f,bit,xxx.i);
    // 再计算回来
    if(xxx.f!=0)
    {// xxx.dw ==0 时, 规定 xxx.f 为0
    char sign=xxx.dw & 0x80000000 ? '-' : '+';// 最高位是符号位
    int  exp=(xxx.dw & 0x7F800000)>>23;//以后8位是指数部分,要减127
    float mantissa=(float)(xxx.dw & 0x007FFFFF)/0x007FFFFF+1;//+1 隐藏的
    printf("Sign=%C,Exponent=2^%d,mantissa=%f\n",sign,exp-127,mantissa);
    }
    }
    //
    float y=0.0;
    for(;;)
    {
    printf ("输入一个浮点数, 输入字符退出.\n");
    if (scanf("%f",&y)==0) return 1;
    xxx.f=(float)y;// change this for other data
    itoa(xxx.i,bit,2);//整数转换为二进制字串
    strrev(bit);// 翻转字串
    unsigned int len=strlen(bit);
    for (int jj=0;jj<(int)(32-len);jj++)
    {// 补零
    strcat(bit,"0");
    }
    strrev(bit);// 恢复
    MBF(xxx);
    printf("I3E: %f = %32s = %08X\n",xxx.f,bit,xxx.i);

    //
    return 0;
    }