Converting float to char array in C / C ++

6

Recently I ran a test and there was an issue that asked for a number like 123.456 to be displayed as 321.456 . I thought the best solution would be to convert this to an array of char and then create an algorithm to print position by position, using pointers, with . and float served as stop tokens. I've implemented the algorithm, but I'm not sure how to do this conversion from a char to an array of float so that each digit is in one position.

How do I convert a char to an array of C/C++ into int ?

I was also curious how I can do this with other guys. char to byte , char to char and do the opposite process also ( float to %code% ).

    
asked by anonymous 03.04.2014 / 18:42

4 answers

7

Although the other answers solve the problem, I put this question in the test just to force engineering scholars to think of a mathematical solution. This is simpler, more natural, and efficient than calling functions to manipulate strings .

Notice that what this exercise actually called for was to change the digits of the unit and the hundred of the number. Well, this can be done as follows:

float num = 123.456f;
int parte_inteira = num;
float parte_decimal = num - parte_inteira;

int digito_centena = parte_inteira / 100;
int digito_dezena = parte_inteira % 100 / 10;
int digito_unidade = parte_inteira % 10;

float num_invertido = (digito_unidade*100) + (digito_dezena*10) + digito_centena + parte_decimal;
printf("%0.3f\n", num_invertido);

I think the code above is quite educational and so I will not go into more detail.

Of course, these operations could be performed on the same line with the help of explicit conversions of data types (cast), saving variable declarations:

float num_invertido = (((int)num % 10) * 100) +
                      (((int)num % 100 / 10) * 10) +
                       ((int)num / 100) +
                       (num - (int)num);
printf("%0.3f\n", num_invertido);
    
06.04.2014 / 00:28
8

In C , you can do:

#include <string.h>
#include <stdio.h>

int main(){

    float valor = 123.456;
    char convertido[16];
    sprintf(convertido, "%.3f", valor);

    printf("A float convertido = %s", convertido);

    return 0;
}

Detail for %.3f , indicating 3 decimal places. Using only %f , conversions can occur in addition to what you want type 123.45600001 in your char[] ;

The sprinf can be used for other types as well ( int , char ), you just have to change the parameter %f to what you want.

In C ++ 11:

#include <iostream>
#include <string>

int main(){

    float valor = 123.456;
    std::string resultado = std::to_string(valor);
    const char* convertido = resultado.c_str();

    std::cout << "O float convertido = " << convertido << std::endl;

    return 0;
}

Also works for int , char , and so on.

In older versions of C ++:

#include <iostream>
#include <string>
#include <sstream>

template <typename T>
std::string to_string(const T a_value)
{
    std::ostringstream out;
    out << a_value;
    return out.str();
}

int main(){

    float valor = 123.456;

    std::string resultado = to_string(valor);
    const char* convertido = resultado.c_str();
    std::cout << "O float convertido = " << convertido << std::endl;
    std::cin.get();

    return 0;
}
    
03.04.2014 / 18:56
5

In pure C, you only have to use the sprintf function that works in the same way as printf , but instead of sending the output to the console you can save it to a variable.

In C

#include <stdio.h>

#define WORD_LENGTH 64

int main(int argc, char *argv[])
{
    float value = 123.456F;
    char str[WORD_LENGTH];

    sprintf(str, "%.3f", value);

    printf("%s\n", str);

    return 0;
}

In C++ you have the freedom to use the same solution as C , but it also has its own output.

#include <iostream>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
    float value = 123.456F;
    stringstream stream;
    string output;

    stream << value;

    output = stream.str();

    cout << output << endl;

    return 0;
}

Already in C++11 the latest version, you can simply use.

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    float value = 123.456F;
    string output = std::to_string(value);

    cout << output << endl;

    return 0;
}
    
03.04.2014 / 19:11
3

One possible solution would be:

float flt = 123.456;
char mybuff1[50];
sprintf (mybuff1, "%f", flt);
char *c [] = {mybuff1};

You have a post explaining better on stackoverflow.com: how can i assign float value to char * c [] array Hope it helps.

    
03.04.2014 / 18:49