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 CCS Technical Support

Float to char ? Printf?

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







Float to char ? Printf?
PostPosted: Fri Feb 28, 2003 7:49 am     Reply with quote

Hi everybody,

I tried to use the printf function to convert my float to a char. With the parameter "fname" of printf i tried to call another function but it seem that calling the function by this way doesn't pass any parameters to that function. Here is a part of the code i use for testing :

printf(Send,"\%C\%C",TempFloat, HumidityFloat);

void Send(char temp, char humi)
{
char cData[8];

cData[0] = temp;
cData[1] = humi;

CanSendMessage(ID_LOW,ID_HIGH, DATA_LENGTH, cData);
}
The float numbers came from the computed data received from a humidity and temperature sensor and i need to send it to a host computer via CAN bus. So i need to have the float converted to char.

Calling the Send function with the printf command this way is not supposed to pass the TempFloat and HumidityFloat converted to char (by the \%C\%C) to the Send function??

I somebody have another suggestion please tell me!

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







Re: Float to char ? Printf?
PostPosted: Fri Feb 28, 2003 8:53 am     Reply with quote

:=Hi everybody,
:=
:=I tried to use the printf function to convert my float to a char. With the parameter "fname" of printf i tried to call another function but it seem that calling the function by this way doesn't pass any parameters to that function. Here is a part of the code i use for testing :
:=
:=printf(Send,"\%C\%C",TempFloat, HumidityFloat);
:=
:=void Send(char temp, char humi)
:={
:= char cData[8];
:=
:= cData[0] = temp;
:= cData[1] = humi;
:=
:= CanSendMessage(ID_LOW,ID_HIGH, DATA_LENGTH, cData);
:=}
:=The float numbers came from the computed data received from a humidity and temperature sensor and i need to send it to a host computer via CAN bus. So i need to have the float converted to char.
:=
:=Calling the Send function with the printf command this way is not supposed to pass the TempFloat and HumidityFloat converted to char (by the \%C\%C) to the Send function??
:=
:=I somebody have another suggestion please tell me!
:=
:=Thanks
The function called by printf, is only meant to take a single variable, not the two shown. It will be called once for each character in the output.
Now converting a float to a char like this, will only make sense if the float is effectively an integer value, and you then have to be conscious of the possibilities of rounding errors as well.
So you would have to do something like:
//as a global
int flag;

void reset_pointer(void) {
flag=0;
}

void Send(char val)
{
static char cData[8];
cData[flag++] = val;
if (flag==2) CanSendMessage(ID_LOW,ID_HIGH, DATA_LENGTH, cData);
}

reset_pointer();
printf(Send,"\%C\%C",(int)(TempFloat+0.4999), (int)(HumidityFloat+0.4999));

This way, the values will be rounded better to the integer characters, and the 'Send' routine will be called twice, the first time with the result of taking the integer part of TempFloat+0.4999, and the second time with the same conversion on HumidityFloat.
The 'reset_pointer' call, resets the counter used inside the Send routine, so the first call places it's char into the first entry in the array, and returns. The second call, puts it's value into the second array entry. Then the test 'flag==2', is true, and the pair of bytes are output.

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







Re: Float to char ? Printf?
PostPosted: Fri Feb 28, 2003 8:56 am     Reply with quote

:=:=Hi everybody,
:=:=
:=:=I tried to use the printf function to convert my float to a char. With the parameter "fname" of printf i tried to call another function but it seem that calling the function by this way doesn't pass any parameters to that function. Here is a part of the code i use for testing :
:=:=
:=:=printf(Send,"\%C\%C",TempFloat, HumidityFloat);
:=:=
:=:=void Send(char temp, char humi)
:=:={
:=:= char cData[8];
:=:=
:=:= cData[0] = temp;
:=:= cData[1] = humi;
:=:=
:=:= CanSendMessage(ID_LOW,ID_HIGH, DATA_LENGTH, cData);
:=:=}
:=:=The float numbers came from the computed data received from a humidity and temperature sensor and i need to send it to a host computer via CAN bus. So i need to have the float converted to char.
:=:=
:=:=Calling the Send function with the printf command this way is not supposed to pass the TempFloat and HumidityFloat converted to char (by the \%C\%C) to the Send function??
:=:=
:=:=I somebody have another suggestion please tell me!
:=:=
:=:=Thanks
:=The function called by printf, is only meant to take a single variable, not the two shown. It will be called once for each character in the output.
:=Now converting a float to a char like this, will only make sense if the float is effectively an integer value, and you then have to be conscious of the possibilities of rounding errors as well.
:=So you would have to do something like:
:=//as a global
:=int flag;
:=
:=void reset_pointer(void) {
:= flag=0;
:=}
:=
:=void Send(char val)
:={
:= static char cData[8];
:= cData[flag++] = val;
:= if (flag==2) CanSendMessage(ID_LOW,ID_HIGH, DATA_LENGTH, cData);
:=}
:=
:=reset_pointer();
:=printf(Send,"\%C\%C",(int)(TempFloat+0.4999), (int)(HumidityFloat+0.4999));
:=
:=This way, the values will be rounded better to the integer characters, and the 'Send' routine will be called twice, the first time with the result of taking the integer part of TempFloat+0.4999, and the second time with the same conversion on HumidityFloat.
:=The 'reset_pointer' call, resets the counter used inside the Send routine, so the first call places it's char into the first entry in the array, and returns. The second call, puts it's value into the second array entry. Then the test 'flag==2', is true, and the pair of bytes are output.
:=
:=Best Wishes
I meant to also say, that this is an uneccessarily complex way of doing the conversion.
Simply saying:

cdata[0]=(int)(tempFloat+0.4999);
cdata[1]=(int)(HumidityFloat+0.4999);
CanSendMessage(ID_LOW,ID_HIGH, DATA_LENGTH, cData);

is much simpler.
You only want to use Printf, where there is formatting required...

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 12215
Hans Wedemeyer
Guest







Re: Float to char ? Printf?
PostPosted: Fri Feb 28, 2003 5:31 pm     Reply with quote

Why not use sprintf()

char buf[20]; // some buffer

sprintf(buf,"\%f\%f",TempFloat, HumidityFloat);

CanSendMessage(ID_LOW,ID_HIGH, DATA_LENGTH, buf);
___________________________
This message was ported from CCS's old forum
Original Post ID: 12243
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