Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to convert float data into string In Pic 18Micro _controller

Status
Not open for further replies.

Tsingran

New Member
Hey guys ,it there anyone who knows how to covert the float data into string data.
For example:

float my_data=1.392338;
char str_data[16];

how can we make my_data(1.392338) into str_data[18]? The function sprintf() provide by MCC18 doesn't work.
 
Hey guys ,it there anyone who knows how to covert the float data into string data.
For example:

float my_data=1.392338;
char str_data[16];

how can we make my_data(1.392338) into str_data[18]? The function sprintf() provide by MCC18 doesn't work.

sprintf() doesn't work? Can you post the code that shows it doesn't work? sprintf() has been around for a long time -- that's not say there isn't a broken implementation out there, I'd just be surprised...

Thanks!

Brad
 
To Brad

Hey Brad
this is a correct function in a C IDE
#include<stdio.h>
main(void)
{
float f=561.234567;
char s[20];
sprintf(s,"%f",f);
printf("%s",s);
getch();
}
the s[20] turn out to be "561.234567",but in my project MAPLAB IDE
CODE:
float f=561.234567;
char s[20];
sprintf(s,"%f",f);
if the function sprintf() works ,it would be s[20]="561.234567",yet?
but it displays tha s[20]="...R.E..@",it's not the data i want.
 
here are my codes in my micro_controller:
Data_write(unsigned char pBuffer,unsigned short int DATA,float f)
{
char a[20]="";
sprintf(a,"%f",f);
while(data-->0)
{
*pBuffer=a;
i++;
}
}
I want to convert my float data into string and write it into a Buffer,but fail.Maybe someone can help solve this problem
 
I don't know anything about MCC18, but in all of the compilers I have used, you need to explicitly turn on floating support for printf/sprintf due to the large increase in the library size.

Well, a quick google search show C18 does not implement floating point support for printf, but gives an alternative.
**broken link removed**
 
here are my codes in my micro_controller:
Data_write(unsigned char pBuffer,unsigned short int DATA,float f)
{
char a[20]="";
sprintf(a,"%f",f);
while(data-->0)
{
*pBuffer=a;
i++;
}
}
I want to convert my float data into string and write it into a Buffer,but fail.Maybe someone can help solve this problem


pBuffer is declared as an "unsigned char". Why are you dereferencing it? I think it should be "unsigned char*".

Brad
 
Hey guys ,it there anyone who knows how to covert the float data into string data.
For example:

float my_data=1.392338;
char str_data[16];

how can we make my_data(1.392338) into str_data[18]? The function sprintf() provide by MCC18 doesn't work.

Modded version of the Microchip code.

far char f2[16];
unsigned long solar;

voltfp(solar,f2) // solar voltage in millivolts, f2 char string for the result

Code:
char* voltfp(unsigned long millvolts,char *strprt)              // convert long (millvolts) to voltage string
{
        int                             iw=0,ip=0;
        float                   voltfrak;
        voltfrak=(float)millvolts/1000;
        iw=(long)((float)voltfrak);
        ip=(long)((float)voltfrak*100)-iw*100;
        sprintf(strprt,"%d.%02d",(int)iw,(int)ip);
        return strprt;
}
 
Last edited:
pBuffer is declared as an "unsigned char". Why are you dereferencing it? I think it should be "unsigned char*".

Brad
The type of pBuffer I define if OK,because another function will use this function ,so that data can be transmit in such form (Socket ,Buffer, length)
 
It's not OK. You can't dereference something that's not a pointer. You haven't defined a buffer, you have defined a character.

Brad
thanks for you advise,but before I use this fuction, I had defined a buffer which address is from 0x0b00 to 0x0dff .So when I use this function ,data will be store bit by bit into the buffer form its origin adddress.
 
Last edited:
thanks for you advise,but before I use this fuction, I had defined a buffer which address is from 0x0b00 to 0x0dff .So when I use this function ,data will be store bit by bit into the buffer form its origin adddress.

But your not referencing in the function. You're taking "unsigned char pBuffer" which is on the stack, dereferencing it (which you shouldn't do) and writing to God knows where.

Brad
 
But your not referencing in the function. You're taking "unsigned char pBuffer" which is on the stack, dereferencing it (which you shouldn't do) and writing to God knows where.

Brad
There are many C files in my project, in my manidemo.c I define such a buffer
#pragma udata SEC_BUF // locate 0x0b00
BYTE Buffer[512]; //unsigned char data area
#pragma udata
then in somewhere of the main file I call for a function
Data_write(Buffer,length);
In another C file I write a function like this
for example:
Data_write (pBuffer ,data)//pBuffer-unsigned char ,data-unsigned int
{
char str[16]="12345678";
unsigned int i=0;
while(data-->0)
{
*pBuffer++=a;
i++;
}
this is a test function in my project ,and it seems realy OK,I can't figure out if there are mistakes.Please give advise , Thank you.

}
 
Modded version of the Microchip code.

far char f2[16];
unsigned long solar;

voltfp(solar,f2) // solar voltage in millivolts, f2 char string for the result

Code:
char* voltfp(unsigned long millvolts,char *strprt)              // convert long (millvolts) to voltage string
{
        int                             iw=0,ip=0;
        float                   voltfrak;
        voltfrak=(float)millvolts/1000;
        iw=(long)((float)voltfrak);
        ip=(long)((float)voltfrak*100)-iw*100;
        sprintf(strprt,"%d.%02d",(int)iw,(int)ip);
        return strprt;
}
Thank you ,this is a good idea ,maybe I should have a try .
 
There are many C files in my project, in my manidemo.c I define such a buffer
#pragma udata SEC_BUF // locate 0x0b00
BYTE Buffer[512]; //unsigned char data area
#pragma udata
then in somewhere of the main file I call for a function
Data_write(Buffer,length);
In another C file I write a function like this
for example:
Data_write (pBuffer ,data)//pBuffer-unsigned char ,data-unsigned int
{
char str[16]="12345678";
unsigned int i=0;
while(data-->0)
{
*pBuffer++=a;
i++;
}
this is a test function in my project ,and it seems realy OK,I can't figure out if there are mistakes.Please give advise , Thank you.

}


Once again, pBuffer is a single character on the stack and it has nothing to do with the variable Buffer you have declared. Just because you call it "pBuffer" doesn't make it a pointer to buffer.

Trying something like this:

Code:
BYTE buffer[512];

#include "otherfile.h"

.
.
.
/* Call function here */
Data_Write(buffer, sizeof(buffer));

where Data_Write() is declared in otherfile.c as:

Code:
#include <string.h>

#include "otherfile.h"

void Data_Write(char* abuffer, int length)
{
  sprintf(abuffer, "%d", 12345678);
}

and you have yet another file called otherfile.h that has:

Code:
#ifndef OTHERFILE_H
#define OTHERFILE_H

void Data_Write(char* abuffer, int length);

#endif /* OTHERFILE_H */

In the above function you already have a buffer, just use it. There's no need to sprintf to a buffer and then copy the buffer to another buffer. Also, length isn't used because sprintf assumes that the buffer is big enough -- this is dangerous. If you pass an uninitialized variable you can quickly overrun buffers. You're better off finding a free version of snprintf() which takes a length and using it.

If you're confused, I would get a good book (K&R, "The C Programming Language").

Brad
 
Status
Not open for further replies.

Latest threads

Back
Top