Helpful ReplyHot!XC16 : Convert Float To String

Page: 12 > Showing page 1 of 2
Author
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
2017/08/13 07:35:48 (permalink)
0

XC16 : Convert Float To String

Hello,
 
I have strange issue with XC16 with below code.

float Test =  12345.6
unsigned char LCDBuffer[20];
sprintf(LCDBuffer,"%.2g ",Test) ;
LCDString((unsigned char *  )  LCDBuffer);

I have got below Errors and Warnings.

main.c: In function 'ConvertFloatToLCDString ': main.c:358:2: warning: pointer targets in passing argument 1 of 'sprintf' differ in signedness
main.c: .......lega-c/ stdio.h:110:5:
note: expected 'char *' but argument is of type 'unsigned char *' main.c:358:2:
warning: format '%.2g ' expects type 'double', but argument 3 has type 'float' main.c:358:10:
warning: Taking the address of 'LCDBuffer' may require an extended pointer for this device.

 
warning: Taking the address of 'LCDBuffer' may require an extended pointer for this device.
 
But this working fine in xc8 complier.
Why so? Please advice . Issue or Any other Method to serve above purpore 
 
--
KR
post edited by karan123 - 2017/08/13 07:48:06
#1
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/13 08:01:21 (permalink)
0
Same for Below Line:
printf("%f",SetPowerOfMode1);
Warning:
main.c:345:4: warning: format '%f' expects type 'double', but argument 2 has type 'float'
#2
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/13 08:03:49 (permalink)
0
Why double take %f  for  printf?
#3
NKurzman
A Guy on the Net
  • Total Posts : 14700
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: XC16 : Convert Float To String 2017/08/13 08:26:18 (permalink)
0
What compiler version?
New or legacy-C library?
#4
andersm
Super Member
  • Total Posts : 2180
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: XC16 : Convert Float To String 2017/08/13 08:28:15 (permalink)
+2 (2)
For variadic functions, floats are promoted to doubles. Microchip's compilers have a long history of not being able to agree with themselves about the width of its floating-point types, so I can't say how serious the warning is. If the C library is expecting a 64-bit double, but the compiler is generating a 32-bit "double" then you will have problems.
#5
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/13 10:04:52 (permalink)
0
Thanks andersm

@Neil
XC16 v1. 31,New plibs
#6
NKurzman
A Guy on the Net
  • Total Posts : 14700
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: XC16 : Convert Float To String 2017/08/13 12:28:30 (permalink)
+1 (1)
karan123
Thanks andersm

@Neil
XC16 v1. 31,New plibs

That is not the latest version of the compiler.
not pLibs.  the compiler libraries.  there are two (regular and
Lega-C.  it is a project compiler options check box.).  And in the past both have their own issues. many related to printf()
#7
andersm
Super Member
  • Total Posts : 2180
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: XC16 : Convert Float To String 2017/08/13 14:43:26 (permalink)
+1 (1)
NKurzmanThat is not the latest version of the compiler.

It's the latest available on Microchip's website.
#8
Aussie Susan
Super Member
  • Total Posts : 2877
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/13 20:02:34 (permalink) ☄ Helpfulby karan123 2017/08/16 01:04:45
+1 (1)
This is also a sledge hammer to crack a nut - the 'printf' family of functions take up a lot of ROM and runtime. They can be very useful but (in my opinion) not for cases such as this.
Your 'format string' is always going to be the same so having the sprintf() function parse ad interpret it each time just burns CPU cycles.
That interpretation will always result in calling the same function which does the same work as the 'ftoa()' function. However at compile/link time, the 'printf' family does not know that and so it has to load in all of the conversion functions which just take up ROM.
Susan
#9
NKurzman
A Guy on the Net
  • Total Posts : 14700
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: XC16 : Convert Float To String 2017/08/13 20:56:46 (permalink)
0
andersm
NKurzmanThat is not the latest version of the compiler.

It's the latest available on Microchip's website.

My mistake then.
See what result the other library gives you.
And warnings aside is it functioning correctly.
#10
mlp
boots too small
  • Total Posts : 492
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: Microchip XC8 and XCLM team
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/14 06:34:29 (permalink) ☄ Helpfulby karan123 2017/08/16 02:44:56
+2 (2)
karan123
Why double take %f  for  printf?

Because back in the 1970s, Dennis designed it that way.

Please, before you post, read the Forum Guidelines
To get a useful answer, always state which PIC you are using!
 
Mark (not paid to state the opinions of Microchip Technology Inc.)


#11
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/14 09:02:44 (permalink)
0
Ok Thanks all of you.

 


void putchar(char a)
{
LCDData (a) ;
}
void main()
{
SysInit() ;
float a=123456.85;
printf("Test") ;
printf("%f",a) ;
}


[apart from CPU clocks and ROM usage]
With including stdio.h the below code works
With XC8 Complier but not with xc16 compiler for printing on LCD.

Do I need to download new Complier and plibs.

Any other method to serve this purpose

--
Karan
#12
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 08:15:44 (permalink)
0
Any idea? How to convert float value to array with Negative Sign with xc16 Complier.
#13
CinziaG
rome burns :D
  • Total Posts : 3085
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 08:32:19 (permalink)
0
sorry, what's the question at this point?
#14
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 08:46:37 (permalink)
+1 (1)
Thanks

How to convert float value to string for LCD Display in xc16 Complier?
#15
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 08:49:10 (permalink)
0
With negative sign of float number?
#16
CinziaG
rome burns :D
  • Total Posts : 3085
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 09:08:26 (permalink)
0
Ok Smile
I'd go again with the code you posted first. Maybe use a real double and cast to "normal char", just to skip the warning. It has to work...
#17
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 09:23:07 (permalink)
+1 (1)
I have checked but why "printf" as not working.
the code I have sent code in conjunction with putchar and LCDData.?


Have you checked?
Thankyou
post edited by karan123 - 2017/08/15 09:32:26
#18
KTrenholm
Super Member
  • Total Posts : 182
  • Reward points : 0
  • Joined: 2012/08/08 14:04:23
  • Location: Connecticut, USA
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 09:43:41 (permalink) ☄ Helpfulby karan123 2017/08/16 01:04:28
+1 (1)
If you were to run the following it should perform the conversion from float to char array:
float Test = 12345.6
unsigned char LCDBuffer[20];
sprintf((char *)LCDBuffer,"%.2f ",(double)Test) ;

 
Do you have a debugger to possibly set a breakpoint and check the contents of LCDBuffer after the sprintf?  What's actually in the buffer at that point?
#19
karan123
Super Member
  • Total Posts : 531
  • Reward points : 0
  • Joined: 2009/02/02 11:11:21
  • Location: 0
  • Status: offline
Re: XC16 : Convert Float To String 2017/08/15 09:47:00 (permalink)
0
Thanks

I have PIC Kit - 3 programmer/debugger and I will let you know tomorrow what is exactly
in buffer.
post edited by karan123 - 2017/08/15 09:49:12
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2017 APG vNext Commercial Version 4.5