EDN logo


Design Ideas July 20, 1995

Program converts hex to floating point

John Santic,
Consultant, Frederick, MD


You might need to use floating-point variables in some embedded software algorithms. You'll find this easy to do because all modern C compilers come with a floating-point library. However, when you're debugging a routine, you sometimes need to "peek" or "poke" a floating-point variable when all you have available is a hexadecimal interface. This scenario arises because simple debuggers or monitor programs usually don't know anything about C variable types. The software can only read or write bytes in ASCII hexadecimal.

You can get around this limitation by using a simple program on a PC to convert between ASCII hexadecimal bytes and decimal floating-point numbers. The program takes advantage of the fact that most embedded software compilers support the IEEE 754 floating-point standard, which is the same format the PC supports. You can have exceptions, though, such as DSP chips that support proprietary floating-point formats with internal hardware. In general, if the floating-point support is through a software library, the software probably uses the IEEE format.

To use the program, you type in the name of the program, followed by one or two parameters. The first parameter is always the number you want to convert. If the number contains a decimal point, the program assumes it to be a decimal number, which the routine converts and prints out as ASCII hexadecimal bytes. If the number doesn't contain a decimal point, the software assumes it to be ASCII hexadecimal and converts and prints the number out as a decimal. You must type in the proper number of hexadecimal digits (eight or 16); you can use either uppercase or lowercase.

By default, the program assumes you want 32-bit, single-precision floating-point numbers and that the hexadecimal bytes start with the least significant byte (in IEEE format, the 8 LSBs of the fraction or mantissa). You can enter a second parameter to override the defaults: D for "double" (64-bit, double-precision format), R for "reverse" (most significant byte first--in IEEE format, the sign and exponent), or DR for both. You can use either uppercase or lowercase.

The following are some examples. The first line, starting with "float," is what you enter, and the information following is the program's response.

float 123.456

  • 32-bit 123.456001 = hex 79 E9 F6 42 (LS byte first)

    float 123.456 DR

  • 64-bit 123.4560000000000 = hex 40 5E DD 2F 1A 9F BE 77 (MS byte first)

    float 42f6e979 r

  • hex 42 F6 E9 79 = 123.456001 (32-bit)

    float 1234567890abcdef d

  • hex 12 34 56 78 90 AB CD EF = -3.598709427848316e+230 (64-bit)

    Incidentally, if the result ever says "INF" (which stands for infinity), the hexadecimal value you entered is the IEEE representation of infinity. You might also get a result "NAN" (not a number), which is the IEEE way of presenting a non-numerical result, such as the result of dividing infinity by infinity.

    The program compiles with Borland C++, but it should also work with other PC compilers. Make sure the compiler options specify "normal" floating-point operation--some compilers also support nonstandard floating point for faster operation. In the compressed ZIP files attached to DI_SIG#1737 on the EDN Readers' BBS, you can find the Listing for the conversion program, as well as an executable C++ file. (DI#1737)

    
    
    
    


    | EDN Access | feedback | subscribe to EDN! |
    | design features | design ideas | columnist |


    Copyright © 1995 EDN Magazine. EDN is a registered trademark of Reed Properties Inc, used under license.