Convert a double to char [32]

1

I have a variable of type double: double p = 32.121; and I want to save it in another variable of type char[8]: char j[8]= ""; .

I'm working with VC++ 2008 and C++9 .

I have tried with the own functionality of c++ . So far I have arrived and the code is correct but returns twice the value .

std::string ConvertDoubleToString(double value){
std::stringstream ss;
ss << value;
return ss.str();}

int _tmain(){

double i = 32.987;

    std::string str = ConvertDoubleToString(i);
    cout<<str;

    // ahora lo almacenanos en un char[]
    char j[8];
    strcpy(j,str.c_str());
    std::cout << j;
    Sleep(10000);
    return 0;
}
    
asked by Andermutu 04.07.2017 в 12:00
source

4 answers

3

You can choose to do it the old-fashioned way of C

double p = 32.121;
char j[8];

sprintf(j,"%f",p);

Or you can make use of C ++ own functionality:

std::stringstream stream;
stream << p;

strcpy(j,p.str().c_str());

Although if it is not a requirement to store it in char[8] , it is best to use std::string :

double p = 32.121;

std::stringstream stream;
stream << p;

std::string j = p.str();
std::cout << j;

The question has been updated: edito

const char* ConvertDoubleToString(double value)
{
  std::stringstream ss;
  ss << value;
  const char * str = ss.str().c_str();
  return str;
}

int _tmain()
{
  const char* j=ConvertDoubleToString(i);
  cout<<j;
  Sleep(4000);
  return 0;
}

What's going on here?

Let's see ...

  const char * str = ss.str().c_str();

There you are returning a pointer to an internal structure of a temporary object:

  • ss.str() creates a temporary std::string object
  • [string].c_str() exposes the internal buffer of string
  • after that line the string is destroyed and its internal pointer is no longer valid

That is, what is stored in str should no longer be used.

Solution?

Returns a string:

std::string ConvertDoubleToString(double value)
{
  std::stringstream ss;
  ss << value;
  return ss.str();
}

int _tmain()
{
  std::string str = ConvertDoubleToString(i);
  cout<<str;

  // ahora lo almacenanos en un char[]
  char j[8];
  strcpy(j,str.c_str());
  std::cout << j;
  Sleep(4000);
  return 0;
}
    
answered by 04.07.2017 / 12:08
source
1

If you want to convert in text mode, you have multiple options. The most typical of C ++ is to use the stream of strings, or stringstream . The idea is to enter data in a stream , just as you would do with, for example, cout , and get a text string. For this, we will need a stringstream output, that is, a ostringstream . These classes are in the sstream header.

ostringstream cnvt;

cnvt << 32.121;
cout << cnvt.str() << endl;

If you want to do the binary conversion, you can do it in two ways, both "old-fashioned":

Copy the contents of the memory from one to another :

char bytes[sizeof(double)];
double x = 32.121;

// Copying memory
memcpy( bytes, &x, sizeof(x) );

for(int i = 0; i < sizeof( x ); ++i) {
    cout << bytes[ i ] << ' ';  
}

When copying from the start of x ( &x ), the number of bytes occupied by x ( sizeof(x) ), you ensure you are copying the content whole of the variable. The target array must be the same size, of course.

Use a union

The union is hardly used today, although for this case they fit like a glove. All the members of a union start at the same memory address, so by modifying one of them, you are modifying them all at once.

union Conversor {
    char bytes[sizeof( double )];
    double x;
};

Thus, the modify Converter . x , we obtain the value conveniently decomposed in Converter . bytes .

Conversor cnvt;
cnvt.x = 32.121;;

for(int i = 0; i < sizeof( x ); ++i) {
    cout << cnvt.bytes[ i ] << ' '; 
}

You have the full IDEOne code .

    
answered by 04.07.2017 в 12:19
0

Whenever you can some version of C ++ 11 or higher it is preferable to use to_string () that facilitates the work and without the need to create your own implementation.

int main()
{

    double real = 32.121;

    string sResult = std::to_string(real);
    cout<<sResult<<endl;

    return 0;
}
    
answered by 05.07.2017 в 21:11
-1

Good morning,

Try this:

double a=32.121;
char arr[sizeof(a)];
memcpy(arr,&a,sizeof(a));

I hope it helps you.

    
answered by 04.07.2017 в 12:09