CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Converting Byte String to Float

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Glen Singletary
Guest







Converting Byte String to Float
PostPosted: Wed Aug 21, 2002 2:41 pm     Reply with quote

I am having difficulty casting a string to float. I am using PCW Compiler PCM version 3.033 and MPLAB IDE for win/16 version 5.50.0.

Consider this test program and the results I get when stepping through:

#include <16F877.h>
#device ICD=TRUE
#include <stdlib.h>
#fuses HS,NOWDT
#zero_ram

float ftemp;
int8 temp[4];

void main(void)
{
temp[0]=0x7f;
temp[1]=0x0;
temp[2]=0x0;
temp[3]=0x0;

ftemp = 1.0;
// RAM at temp = 7F 00 00 00
// RAM at ftemp = 7F 00 00 00
// Watch windows shows 1.000+00 for ftemp
ftemp = (float)*temp;
// RAM at temp = 7F 00 00 00
// RAM at ftemp = 85 7E 00 00
// Watch window shows 1.270+02 for ftemp
ftemp = *temp;
// same results...
}

Any ideas on why I am getting this result and how I can fix it?

thanks
___________________________
This message was ported from CCS's old forum
Original Post ID: 6494
R.J.Hamlett
Guest







Re: Converting Byte String to Float
PostPosted: Wed Aug 21, 2002 3:12 pm     Reply with quote

:=I am having difficulty casting a string to float. I am using PCW Compiler PCM version 3.033 and MPLAB IDE for win/16 version 5.50.0.
:=
:=Consider this test program and the results I get when stepping through:
:=
:=#include <16F877.h>
:=#device ICD=TRUE
:=#include <stdlib.h>
:=#fuses HS,NOWDT
:=#zero_ram
:=
:=float ftemp;
:=int8 temp[4];
:=
:=void main(void)
:= {
:= temp[0]=0x7f;
:= temp[1]=0x0;
:= temp[2]=0x0;
:= temp[3]=0x0;
:=
:= ftemp = 1.0;
:= // RAM at temp = 7F 00 00 00
:= // RAM at ftemp = 7F 00 00 00
:= // Watch windows shows 1.000+00 for ftemp
:= ftemp = (float)*temp;
At this point, 'temp' contains the numeric sequence 7F00..., and you are now using this as a pointer!. You need to cast the _address_ of temp, to be a pointer to a float.

:= // RAM at temp = 7F 00 00 00
:= // RAM at ftemp = 85 7E 00 00
:= // Watch window shows 1.270+02 for ftemp
:= ftemp = *temp;
:= // same results...
:= }
:=
:=Any ideas on why I am getting this result and how I can fix it?
:=
:=thanks
You need to be more careful in dealing with pointers. The '&' character gets the address of temp (which you want), and this address, is then used with the '*' character as a pointer. Hence the 'full' expansion would be :

ftemp=*((float *)&temp);

which takes the address of 'temp', and converts this to a pointer to a float, and then uses this pointer to retrieve the value.
You can also use a union to get the same effect.

union {
int8 temp[4];
float ftemp;
} cstore;

cstore.temp[0]=0x7F;
cstore.temp[1]=cstore.temp[2]=cstore.temp[3]=0x00;

Now cstore.ftemp is the floating point value.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 6497
Glen Singletary
Guest







Re: Converting Byte String to Float
PostPosted: Thu Aug 22, 2002 9:06 am     Reply with quote

:=:=I am having difficulty casting a string to float. I am using PCW Compiler PCM version 3.033 and MPLAB IDE for win/16 version 5.50.0.
:=:=
:=:=Consider this test program and the results I get when stepping through:
:=:=
:=:=#include <16F877.h>
:=:=#device ICD=TRUE
:=:=#include <stdlib.h>
:=:=#fuses HS,NOWDT
:=:=#zero_ram
:=:=
:=:=float ftemp;
:=:=int8 temp[4];
:=:=
:=:=void main(void)
:=:= {
:=:= temp[0]=0x7f;
:=:= temp[1]=0x0;
:=:= temp[2]=0x0;
:=:= temp[3]=0x0;
:=:=
:=:= ftemp = 1.0;
:=:= // RAM at temp = 7F 00 00 00
:=:= // RAM at ftemp = 7F 00 00 00
:=:= // Watch windows shows 1.000+00 for ftemp
:=:= ftemp = (float)*temp;
:=At this point, 'temp' contains the numeric sequence 7F00..., and you are now using this as a pointer!. You need to cast the _address_ of temp, to be a pointer to a float.
:=
:=:= // RAM at temp = 7F 00 00 00
:=:= // RAM at ftemp = 85 7E 00 00
:=:= // Watch window shows 1.270+02 for ftemp
:=:= ftemp = *temp;
:=:= // same results...
:=:= }
:=:=
:=:=Any ideas on why I am getting this result and how I can fix it?
:=:=
:=:=thanks
:=You need to be more careful in dealing with pointers. The '&' character gets the address of temp (which you want), and this address, is then used with the '*' character as a pointer. Hence the 'full' expansion would be :
:=
:=ftemp=*((float *)&temp);

Using this expression gave me an "Expecting and Identifier" error because temp was declared as a string. Modifying this slightly to:

ftemp=*((float *)&temp[0]);
or
ftemp=*((float *)temp);

gave the correct results.

:=
:=which takes the address of 'temp', and converts this to a pointer to a float, and then uses this pointer to retrieve the value.
:=You can also use a union to get the same effect.
:=
:=union {
:= int8 temp[4];
:= float ftemp;
:=} cstore;
:=
:=cstore.temp[0]=0x7F;
:=cstore.temp[1]=cstore.temp[2]=cstore.temp[3]=0x00;
:=
:=Now cstore.ftemp is the floating point value.
:=
:=Best Wishes

Thanks for the help.
___________________________
This message was ported from CCS's old forum
Original Post ID: 6518
R.J.Hamlett
Guest







Re: Converting Byte String to Float
PostPosted: Thu Aug 22, 2002 9:24 am     Reply with quote

:=<snipped>
:=:=ftemp=*((float *)&temp);
:=
:=Using this expression gave me an "Expecting and Identifier" error because temp was declared as a string. Modifying this slightly to:
:=
:=ftemp=*((float *)&temp[0]);
:=or
:=ftemp=*((float *)temp);
:=
:=gave the correct results.
At least you saw 'where I was coming from', in that you need the address of the variable, not the contents. :-)
I tend to 'under proofread' what I type at times, so this sort of error is common....

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 6519
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group