![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Apr 2006
Posts: 7
| Combining four hex characters into one floating point number for output? 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! |
| lava is offline | |
| | #2 |
| Guest Join Date: Aug 2001
Posts: 5,034
| 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));
|
| Sebastiani is offline | |
| | #3 |
| Guest Join Date: Aug 2001
Posts: 5,034
| not to mention there are no guarantees that your machine will represent floats in the same format as described in your post. |
| Sebastiani is offline | |
| | #4 |
| Registered User Join Date: Apr 2006
Posts: 7
| Thanks, will give it a try! |
| lava is offline | |
| | #5 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,710
| memcpy( &myfloat, number, sizeof(float) ); Simply casting from one pointer type to another pointer type may get you an alignment exception. |
| Salem is offline | |
| | #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));
};
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));
};
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. |
| lava is offline | |
| | #7 |
| Supermassive black hole Join Date: Jul 2005 Location: South Wales, UK
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 |
| ahluka is offline | |
| | #8 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| >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 */
}
>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.
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #9 | |
| Registered User Join Date: Apr 2006
Posts: 7
| Quote:
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);
}
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. | |
| lava is offline | |
| | #10 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| >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));
__________________ http://www.freechess.org |
| swoopy is offline | |
| | #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);
}
|
| lava is offline | |
| | #12 |
| Just Lurking Join Date: Oct 2002
Posts: 5,006
| 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.* |
| Dave_Sinkula is offline | |
| | #13 | |
| Registered User Join Date: Apr 2006
Posts: 7
| Quote:
| |
| lava is offline | |
| | #14 |
| Just Lurking Join Date: Oct 2002
Posts: 5,006
| 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=2]http://www.dragonsgate.net/cgi-bin/FAQ/fom?file=43 ?
__________________ 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.* Last edited by Dave_Sinkula; 04-05-2006 at 09:18 PM. |
| Dave_Sinkula is offline | |
| | #15 | |
| Registered User Join Date: Apr 2006
Posts: 7
| Quote:
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. | |
| lava is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Learning Memory, Ins and Outs? | Zoiked | C Programming | 1 | 08-27-2007 04:43 PM |
| No atoh() function in C ( Ascii To Hex )? - Well, Let's Create One | dedham_ma_man | C Programming | 11 | 03-24-2006 11:26 AM |
| Random Number problem in number guessing game... | -leech- | Windows Programming | 8 | 01-15-2002 05:00 PM |
| Array of boolean | DMaxJ | C++ Programming | 11 | 10-25-2001 11:45 PM |
| Structures and floating point variables (repost with code tags) | Joshua138 | C Programming | 2 | 09-10-2001 12:00 PM |