Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

need a sample of code to convert float to char array

Status
Not open for further replies.

userx51

Junior Member level 2
Junior Member level 2
Joined
Apr 21, 2013
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,432
does any one has any code to convert float to char without using sprintf?.
 

If you use mikroe compilers then it has FloatToStr() function.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//Global Variables
double FP_NUM;
char string[8];
 
//Function Prototypes
void floattostring(double FP_NUM);
 
//Sub Function
void floattostring(double FP_NUM) {
        double fpnumber;
        long int befdec, aftdec;
 
        fpnumber = FP_NUM;
 
        befdec = fpnumber;                      // Fractional part is truncated
                                                // 12.163456 becomes 12
        aftdec = fpnumber * 100;            // 12.163456 becomes 1216
        aftdec = aftdec - (befdec * 100);   // 1216 - 1200 = 16
 
 
        if (fpnumber < 1) {
                string[0] = '0';
                string[1] = '.';
                string[2] = (aftdec/10) + 48;
                string[3] = (aftdec/1)%10 + 48;
                string[4] = ' ';
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';
 
        }
 
        else if ((fpnumber >= 1) && (fpnumber < 10)) {
                string[0] = (befdec/1)%10 + 48;
                string[1] = '.';
                string[2] = (aftdec/10) + 48;
                string[3] = (aftdec/1)%10 + 48;
                string[4] = ' ';
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';
 
        }
 
        else if ((fpnumber >= 10) && (fpnumber < 100)) {
                string[0] = (befdec/10) + 48;
                string[1] = (befdec/1)%10 + 48;
                string[2] = '.';
                string[3] = (aftdec/10) + 48;
                string[4] = (aftdec/1)%10 + 48;
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';
 
        }
 
        else if ((fpnumber >= 100) && (fpnumber < 1000)) {
                string[0] = (befdec/100) + 48;
                string[1] = (befdec/10)%10 + 48;
                string[2] = (befdec/1)%10 + 48;
                string[3] = '.';
                string[4] = (aftdec/10) + 48;
                string[5] = ' ';
                string[6] = ' ';
                string[7] = '\0';
 
        }
        
        else if ((fpnumber >= 1000) && (fpnumber < 10000)) {
                string[0] = (befdec/1000) + 48;
                string[1] = (befdec/100)%10 + 48;
                string[2] = (befdec/10)%10 + 48;
                string[3] = (befdec/1)%10 + 48;
                string[4] = '.';
                string[5] = (aftdec/10) + 48;
                string[6] = ' ';
                string[7] = '\0';
 
        }
 
        else if ((fpnumber >= 10000) && (fpnumber < 100000)) {
                string[0] = (befdec/10000) + 48;
                string[1] = (befdec/1000)%10 + 48;
                string[2] = (befdec/100)%10 + 48;
                string[3] = (befdec/10)%10 + 48;
                string[4] = (befdec/1)%10 + 48;
                string[5] = '.';
                string[6] = (aftdec/10) + 48;
                string[7] = '\0';
 
        }
        
 
}
 
void main() {
      
      while(1) {
 
                      
           volt = 123.2364
                     
 
           floattostring(volt);
           Lcd_Out(1,1,string);
                     
 
      }
      
}

 

Float to char or float to string??
why not using sprintf??
using Spirintf in a program could crash your program any time, better choice is using snprintf..
many compilers doesnt support float printing in printf so the easiest way is converting the float into a decimal number by multiplying it

Code C - [expand]
1
2
3
4
5
float x = 34.567;
int a,b;
a = (int)x;
b = ((int)( x * 1000)) % 1000;
snprintf(str,8,"%d.%0.3d",a,b);


print.JPG
without using two this, your going to write lot of lines........
 
Last edited:

Hello,

many compilers doesnt support float printing in printf so the easiest way is converting the float
into a decimal number by multiplying it
Code C - [expand]

1
2
3
4
5
float x = 34.567;
int a,b;
a = (int)x;
b = ((int)( x * 1000)) % 1000;
snprintf(str,8,"%d.%0.3d",a,b);


without using two this, your going to write lot of lines........

Yes, it's a very fast and simple solution,
but limited to 32767.0 and -32768.0 value
or use long int for a,b
 
Last edited:

no mikroe compilers, no snprintf works. It takes a more low level c code.
 

no mikroe compilers, no snprintf works. It takes a more low level c code
Every C compiler has some kind of itoa() conversion or numeric print feature. And it's highly suggested to use thes built-in library functions, because a self-elaborated conversion as shown in post #2 maybe needs the tenfold code space.
 

hello,

I checked it, you can not use code in post #3 for floating value over 32.767
maybe depend of what is an int ? for 8 bits µcontroller (as 16F or 18F) int is signed 16 bits , so -32768 to + 32767
if tested other soluce with MPLAB C18 , but near standard C.

Result get on watch register window is included inside this code , as comment.
You can see that that to handle floating and long int conversion is not so evident,
for big numbers.

Code:
// MPLAB C18 
// Test sur convertion Float to ASCII
// pieges sur arrondi
// Test_flot_2_string_130927.c_


#include "p18f26k22.h"
#define p18f26k22
#define Versus "Versus 130926 C18   "


#include "stdio.h"
#include <stdlib.h>
#include <string.h>
#include <math.h>


//#define OSCILLATEUR_INTERNE

#ifdef OSCILLATEUR_INTERNE
#pragma config FOSC = INTIO67, FCMEN = OFF, PLLCFG=OFF            // CONFIG1H
#else
#pragma config FOSC = HSHP, PLLCFG=OFF, PRICLKEN=ON ,FCMEN=OFF      // quartz 10Mhz
#endif
#pragma config IESO=OFF,PWRTEN=OFF,BOREN=OFF,WDTEN=OFF,CCP2MX=PORTC1
#pragma config PBADEN=OFF,T3CMX=PORTC0,P2BMX=PORTC0,CCP3MX=PORTB5
#pragma config MCLRE=EXTMCLR,STVREN=OFF,LVP=ON,XINST=OFF,DEBUG=OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF                    // CONFIG5L
#pragma config CPB = OFF, CPD = OFF                                          // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF                // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF                            // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF            // CONFIG7L
#pragma config EBTRB = OFF                                                   // CONFIG7H


// Using Q=20Mhz  Clock 
#define CLOCK_FREQ 20000000L 
#define FOSC 10000000


#ifndef Byte
#define Byte unsigned char
#endif

char str[32];
float x,y ,z;
long int d,e; 
int  a,b;
int k;

void main()
{

//=========Zone for short tests===================
x= 34.567;
a = (int)x;
b = ((int) (x * 1000)) % 1000;
k=sprintf(str,"%8d%1c%3d",a,46,b);
/*  Problem if over 32.0
          6C8 str                          "      34.-969"
          6FC a                                         34
          6FE b                                       -969
 */         
x= 32.767;
a = (int)x;
b = ((int) (x * 1000)) % 1000;
k=sprintf(str,"%8d%1c%3d",a,46,b);
/*  here no problemo because  32*1000 < 32767
          6C8 str                          "      32.766"
          6FC a                                         32
          6FE b                                        766
 */ 
 
x= 34.567;
d = (long int)x;
e = ((long int) (x * 1000.0)) % 1000;
k=sprintf(str,"%8ld%1c%3ld",d,46,e);
/*  OK 
          6C8 str                          "        34.567"
          6F4 d                                        34
          6F8 e                                       567
 */  
 
 
 
x= 1234567.876;
d = (long int)x;
e = ((long int) (x * 1000.0)) % 1000;
k=sprintf(str,"%8ld%1c%3ld",d,46,e);
/*  Erreur in decimal part
          6C8 str                          "  1234567.936"
          6F4 d                                   1234567
          6F8 e                                       936
 */   

      
x= 1234567.876; 
y= modf(x,&z);    // <-  used to separate entire part and decimal part  see C18 Maths
d=(long int)z;
e=(long int)(y*1000.0);
k=sprintf(str,"%8ld%1c%3ld",d,46,e);
/*  OK, only rounded at 1/1000 decimale
          6C8 str                          "  1234567.875"
          6F4 d                                   1234567
          6F8 e                                       875
 */   


while(1);
 

hello,
I checked it, you can not use code in post #3 for floating value over 32.767
maybe depend of what is an int ? for 8 bits µcontroller (as 16F or 18F) int is signed 16 bits , so -32768 to + 32767
if tested other soluce with MPLAB C18 , but near standard C.

I have just shown an Idea and I believe you can use that method for any float value with any compiler... Nothing is impossible with C...
and also thanks for constructing such a test case.....

and here is some more better idea..

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
main()
{
float x = 34.567;
 
int a,b;
float c;
 
a = (int)x;                     // taking integral part
c = x - (int) x;                // taking fractional part
b = ((int)( c * 1000)) % 1000;    // fractional part to intger
 
printf("%d.%0.3d",a,b);
}


print.jpg

- - - Updated - - -

For OP's problem

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
void float_to_string(float x, char* string)
{
                string[0] = (int) ( x / 1000 ) % 10 + '0';
                string[1] = (int) ( x / 100  ) % 10 + '0';
                string[2] = (int) ( x / 10   ) % 10 + '0';
                string[3] = (int) ( x / 1    ) % 10 + '0';
                string[4] = '.';
                string[5] = (int) ( x * 10   ) % 10 + '0';
                string[6] = (int) ( x * 100  ) % 10 + '0';
                string[7] = (int) ( x * 1000 ) % 10 + '0';
                string[8] = '0';
                string[9] =  0 ;
}


you can use this function but the decimal places will be fixed....
attachment.php
 

Attachments

  • flaot.JPG
    flaot.JPG
    59.6 KB · Views: 727
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top