C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-02-2006, 10:01 PM   #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!
lava is offline   Reply With Quote
Old 04-02-2006, 10:21 PM   #2
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 04-02-2006, 10:23 PM   #3
Guest
 
Sebastiani's Avatar
 
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   Reply With Quote
Old 04-02-2006, 10:27 PM   #4
Registered User
 
Join Date: Apr 2006
Posts: 7
Thanks, will give it a try!
lava is offline   Reply With Quote
Old 04-03-2006, 02:40 AM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 04-05-2006, 01:30 PM   #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.
lava is offline   Reply With Quote
Old 04-05-2006, 01:36 PM   #7
Supermassive black hole
 
ahluka's Avatar
 
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   Reply With Quote
Old 04-05-2006, 01:46 PM   #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 */
}
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.
__________________
http://www.freechess.org
swoopy is offline   Reply With Quote
Old 04-05-2006, 02:04 PM   #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.
lava is offline   Reply With Quote
Old 04-05-2006, 05:43 PM   #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   Reply With Quote
Old 04-05-2006, 08:30 PM   #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?
lava is offline   Reply With Quote
Old 04-05-2006, 08:39 PM   #12
Just Lurking
 
Dave_Sinkula's Avatar
 
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   Reply With Quote
Old 04-05-2006, 09:04 PM   #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.
lava is offline   Reply With Quote
Old 04-05-2006, 09:06 PM   #14
Just Lurking
 
Dave_Sinkula's Avatar
 
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]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 ?
__________________
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   Reply With Quote
Old 04-06-2006, 12:18 AM   #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.
lava is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22