This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

about the convert of float

I want to convert the hex float value in memory into decimal,such as 42F94000H=124.75,which 42F94000H is the hex value,and how can I implement the decimal 124.75 using program,or other cases?

Parents
  • Other than using a library routine - where would be the fun in that - you will need to write your own code. This should get you started.

    First, you need to understand how floating point values are represented. See:

    http://www.ahpcc.unm.edu/~acpineda/CS471/HTML/ieeeflpt/

    The 32-bit value you have in hex is divided into three fields. The first field is of one bit and indicates the sign of the value. The second field is of eight bits and indicates the exponent plus 127 and the remainder is the fractional part with an implied one as an extra most-significant bit.

    42F94000 
    = 0100 0010 1111 1001 0100 0000 0000 0000
      ||        ||                          |
      ||        |+-------fraction-----------+
      |+exponent+
      +sign
    
    sign = 0 indicating positive value.
    exponent = 10000101 = 133. 133-127=6
    fraction = 111 1001 0100 0000 0000 0000
    
    To conver to a decimal, it is necessary to normalise the floating point representation. If your range of values you wish to convert is not too great (exponent between +10 and -10 say), the first step should be to make a conversion to fixed binary-point representation. You can choose where to put the binary point, sacraficing range for resolution.

    Write the fractional part to a long int:
    0000 0000 0111 1001 0100 0000 0000 0000
    
    Shift the fractional part until it is correctly alligned with your chosen position for the fixed binary-point (bold).
    0000 0000 0000 0000.1111 0010 1000 0000
    
    Add the implied (in bold) msb set to one:
    0000 0000 0000 0001.1111 0010 1000 0000
    
    Shift the long int the number of times indicated by the value of the exponent. Positive exponent shifts left and negative exponent shifts right.

    In this case the exponent is 6, so shift six places left.
    0000 0000 0111 1100.1010 0000 0000 0000
    
    If you need speed, you can write a variant of the code placed here:
    http://www.keil.com/forum/docs/thread1252.asp . The code described is a roll, you need a shift.

    All that is now needed is a binary to decimal conversion. You will find some code given in this thread: http://www.keil.com/forum/docs/thread890.asp . You will need to do a little work to change it to fixed decimal place - easy. Use the sign bit to determine '+' or '-' prefix.

    Note that you will have to make special provision for IEEE represention of zero.

    I make your floating point value 127.625. Did I go wrong somewhere?

Reply
  • Other than using a library routine - where would be the fun in that - you will need to write your own code. This should get you started.

    First, you need to understand how floating point values are represented. See:

    http://www.ahpcc.unm.edu/~acpineda/CS471/HTML/ieeeflpt/

    The 32-bit value you have in hex is divided into three fields. The first field is of one bit and indicates the sign of the value. The second field is of eight bits and indicates the exponent plus 127 and the remainder is the fractional part with an implied one as an extra most-significant bit.

    42F94000 
    = 0100 0010 1111 1001 0100 0000 0000 0000
      ||        ||                          |
      ||        |+-------fraction-----------+
      |+exponent+
      +sign
    
    sign = 0 indicating positive value.
    exponent = 10000101 = 133. 133-127=6
    fraction = 111 1001 0100 0000 0000 0000
    
    To conver to a decimal, it is necessary to normalise the floating point representation. If your range of values you wish to convert is not too great (exponent between +10 and -10 say), the first step should be to make a conversion to fixed binary-point representation. You can choose where to put the binary point, sacraficing range for resolution.

    Write the fractional part to a long int:
    0000 0000 0111 1001 0100 0000 0000 0000
    
    Shift the fractional part until it is correctly alligned with your chosen position for the fixed binary-point (bold).
    0000 0000 0000 0000.1111 0010 1000 0000
    
    Add the implied (in bold) msb set to one:
    0000 0000 0000 0001.1111 0010 1000 0000
    
    Shift the long int the number of times indicated by the value of the exponent. Positive exponent shifts left and negative exponent shifts right.

    In this case the exponent is 6, so shift six places left.
    0000 0000 0111 1100.1010 0000 0000 0000
    
    If you need speed, you can write a variant of the code placed here:
    http://www.keil.com/forum/docs/thread1252.asp . The code described is a roll, you need a shift.

    All that is now needed is a binary to decimal conversion. You will find some code given in this thread: http://www.keil.com/forum/docs/thread890.asp . You will need to do a little work to change it to fixed decimal place - easy. Use the sign bit to determine '+' or '-' prefix.

    Note that you will have to make special provision for IEEE represention of zero.

    I make your floating point value 127.625. Did I go wrong somewhere?

Children