Floating-point numbers converted into string functions

Source: Internet
Author: User

sprintf function is too large, in the STM8 above is not afraid to use, is still. Text overflow. In order to upload the collected values through the serial port to the computer, you can only write a floating-point number into a string function:

#include <stdio.h> #include <stdint.h>static char table[]={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};vo ID Num2char (char *str, double number, uint8_t G, uint8_t l) {    uint8_t i;    int temp = NUMBER/1;    Double t2 = 0.0;    for (i = 1; i<=g; i++)    {        if (temp==0)            str[g-i] = table[0];        else            str[g-i] = table[temp%10];        temp = TEMP/10;    }    * (str+g) = '. ';    temp = 0;    t2 = number;    for (I=1; i<=l; i++)    {        temp = t2*10;        Str[g+i] = table[temp%10];        t2 = t2*10;    }    * (str+g+l+1) = ' + ';} int main (int argc, char const *argv[]) {    char str[20];    Num2char (str, 23.56821312, 8, ten);    printf ("%s\n", str);    return 0;}

The test results are as follows:

00000023.5682131

Floating-point numbers converted into string functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.