Thread: Combining four hex characters into one floating point number for output?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    7

    Combining four hex characters into one floating point number for output?

    If I have an array, number[4], of these values:

    number[0]=0x3F
    number[1]=0xC7
    number[2]=0xAE
    number[3]=0x14

    I want to use a printf(%f) statement to combine this array into a single number (0x3FC7AE14), and then output that as a single floating-point number (1.56).

    The printf statement is the easy part. My problem is I do not know how to take the four separate array values and combine them into a single floating point number. Can someone tell me how to do this?

    Thanks!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    assuming 'number' is an array of char AND sizeof(float) == 4 on your system AND your machine uses big-endian ordering:

    Code:
    printf("%f\n", *((float*)number));
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    not to mention there are no guarantees that your machine will represent floats in the same format as described in your post.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Thanks, will give it a try!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    memcpy( &myfloat, number, sizeof(float) );

    Simply casting from one pointer type to another pointer type may get you an alignment exception.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    I can't get either of these solutions to work. I tried this code:

    Code:
    char number[4];
    sizeof(float)==4;
    
    while (1)
      {
      number[0]=0x3F;
      number[1]=0xC7;
      number[2]=0xAE;
      number[3]=0x14;
      printf("%f\n", *((float*)number));
      };
    With this code I get a "declaration syntax error" on the sizeof line. I then tried this code instead:

    Code:
    char number[4];
    float float_number;
    sizeof(float_number)==4;
    
    while (1)
      {
      number[0]=0x3F;
      number[1]=0xC7;
      number[2]=0xAE;
      number[3]=0x14;
      printf("%f\n", *((float_number*)number));
      };
    With this code I still get a "declaration syntax error" on the sizeof line, and I also get an "invalid expression" error on the printf line.

    As for the second solution using memcpy, my compiler won't recognize it. It may have to do with the fact that I'm writing this code for an Atmel microcontroller.

    Any further help is appreciated!
    Last edited by lava; 04-05-2006 at 01:59 PM.

  7. #7
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    sizeof(float_number) is not an l-value AFAIK.

    EDIT: Is it? And use CODE tags please.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >sizeof(float)==4;

    sizeof returns the size of whatever you pass it. You have to assign the result to some variable. It looks like you're trying to check for what is the size of a float. In that case use an if() statement:
    Code:
    if (sizeof(float) == 4)
    {
       /* Do something here */
    }
    For now, you can probably just remove that line, and it will compile.

    >As for the second solution using memcpy, my compiler won't recognize it.
    Are you sure you included the proper header file (string.h)? This solution is much preferred, as it is a portable solution.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Quote Originally Posted by swoopy
    Are you sure you included the proper header file (string.h)? This solution is much preferred, as it is a portable solution.
    Thanks swoopy, I was not including string.h. I included it, and then tried this code:

    Code:
    char number[4]; 
    float float_number; 
    
    while(1)
      {
      number[0]=0x3F;
      number[1]=0xC7;
      number[2]=0xAE;
      number[3]=0x14;
      memcpy(float_number, number, 4);   
      printf("%f\n", float_number);  
      }
    I got this error with regards to the memcpy line:

    parameter #1 of type 'float' is incompatible with type 'void*' specified in the function 'memcpy' declaration

    I feel like I'm almost there.
    Last edited by lava; 04-05-2006 at 02:07 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >parameter #1 of type 'float' is incompatible with type 'void*' specified in the function 'memcpy' declaration

    Ok, if you check Salem's post, he passes an address for the first argument. This is required, because otherwise you would simply be passing a copy of the float variable, and nothing would be changed. So add & to your code:
    Code:
      memcpy(&float_number, number, sizeof(float_number));

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Okay, I tried this code:

    Code:
    char number[4];         
    float output;
       
    while(1)
      {
      number[0]=0x3F;
      number[1]=0xC7;
      number[2]=0xAE;
      number[3]=0x14;
      memcpy(&output,number,4); 
      printf("The floating point number is %f\n\r", output);
      }
    And it compiles without errors. But when I watch the result in a terminal window it says "The floating point number is" over and over again with a carriage return and line feed at the end. When I look at the output in hex, there are no characters whatsoever between the space after the word "is" and the \n\r. It goes right from 0x20 (space) to 0x0A & 0x0D (LF & CR) without including anything in between. Shouldn't the hex values of the floating point number be there?

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps your endianness is reversed.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       float output;
       char number[4];
       number[3]=0x3F;
       number[2]=0xC7;
       number[1]=0xAE;
       number[0]=0x14;
       memcpy(&output, number, sizeof number);
       printf("output = %g\n", output);
       return 0;
    }
    
    /* my output
    output = 1.56
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Quote Originally Posted by Dave_Sinkula
    Perhaps your endianness is reversed.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       float output;
       char number[4];
       number[3]=0x3F;
       number[2]=0xC7;
       number[1]=0xAE;
       number[0]=0x14;
       memcpy(&output, number, sizeof number);
       printf("output = %g\n", output);
       return 0;
    }
    
    /* my output
    output = 1.56
    */
    Weird. I just copied your code into my compiler and onto the microcontroller, and all I got was "output =", no number after. I have no idea what is wrong.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about trying the reverse case first?
    Code:
    #include <stdio.h>
    
    int main()
    {
       float value = 1.56;
       unsigned char *byte = (unsigned char*)&value;
       size_t i;
       for ( i = 0; i < sizeof value; ++i )
       {
          printf("byte[%lu] = 0x%02X\n", (long unsigned)i, (unsigned)byte[i]);
       }
       return 0;
    }
    
    /* my output
    byte[0] = 0x14
    byte[1] = 0xAE
    byte[2] = 0xC7
    byte[3] = 0x3F
    */
    [edit]BTW, which micro, compiler? Is there some compiler switch that leaves out floating point formats for printf unless you explicitly request it?

    [edit=2]http://www.dragonsgate.net/cgi-bin/FAQ/fom?file=43 ?
    Last edited by Dave_Sinkula; 04-05-2006 at 09:18 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    7
    Quote Originally Posted by Dave_Sinkula
    BTW, which micro, compiler? Is there some compiler switch that leaves out floating point formats for printf unless you explicitly request it?
    I'm using an Atmel ATmega32 with CodeVisionAVR. I checked the settings, and what do you know, yes, the default setting does NOT allow printf to output floats. So, I turned it on, and... "Evaluation version file size limit exceeded" Dang!

    So now I have to buy the full version. But, nice catch man, thanks! I bet that fixes it.

    And thanks to everybody else who helped. This is a great forum, and hopefully once I'm a bit more experienced I can contribute more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM