Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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.
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
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.
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;
}
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)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)
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.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.
There are many C files in my project, in my manidemo.c I define such a bufferBut 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
Thank you ,this is a good idea ,maybe I should have a try .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; }
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.
}
BYTE buffer[512];
#include "otherfile.h"
.
.
.
/* Call function here */
Data_Write(buffer, sizeof(buffer));
#include <string.h>
#include "otherfile.h"
void Data_Write(char* abuffer, int length)
{
sprintf(abuffer, "%d", 12345678);
}
#ifndef OTHERFILE_H
#define OTHERFILE_H
void Data_Write(char* abuffer, int length);
#endif /* OTHERFILE_H */