Convert Array of Double to String

Sep 17, 2011 at 6:44pm
I found C++ code somewhere out there, that makes me interesting.
array of double is printed as a string.
how can I do like that??
give me some advice please...
here is example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>
 
using namespace std;
 
int main(int argc, char** argv) {
 
    double x[] = {        
        4.2232015408671989833492853721E+257,
        2.68903651820394743790189391339E+161,
        6.20198216450250504635967326839E+223,
        1.23988366443015857155592322383E+224,
        1.36702301190817523125986798132E+161,
        2.25033482836218612841162443725E-307                                        
      };    
    
    printf((char*)x);
    
    return 0;
}
Last edited on Sep 18, 2011 at 6:39pm
Sep 18, 2011 at 6:40pm
Please somebody tell me!!!
Sep 18, 2011 at 7:26pm
 
printf((char*)x);//<-- this is pure evil. 


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
int main()
{
  double a = 3.0;
  int b = 2;
  char c = 'a';
  std::cout<<"C++ allows you to easily print stuff without having to convert it yourself, for example a double like "<<a<<std::endl
                 "or a int like "<<b<<" or just a plain old char like "<<c<<" ... Or well, C strings like most of this text."<<std::endl;
  std::cout<<"With operator overloading you can even do this for your own datatypes, but this may be a bit beyond the you at the moment"<<std::endl;
  return 0;
}
Last edited on Sep 18, 2011 at 7:26pm
Sep 18, 2011 at 8:35pm
For the curious the OP's program prints the following:

punya ludevil sama keren ama punya marcadian :D

But why?

Firstly, what was printed is not the "doubles as a string", but rather their raw bytes cast to a C-String. Notice how there are 48 characters in the printed string (47 visible characters + 1 for the null character). Also notice that the 6 doubles take up 48 bytes of memory (assuming 8 bytes per double).

Now, what the original author of that code did was something like the following:

1. Take the first eight characters, e.g. "punya lu"
2. Place those eight characters into the memory location of a double and store the double
3. Repeat for all the characters. If the phrase is not a multiple of 8 characters, pad the end with null characters

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
    const int NUM_DOUBLES_IN_PHRASE = 6;
    const char* phrase = "punya ludevil sama keren ama punya marcadian :D";

    double phraseAsDoubles[NUM_DOUBLES_IN_PHRASE];

    for(int i = 0; i < NUM_DOUBLES_IN_PHRASE; i++)
    {
        const double* addressAsDouble = (const double*) (phrase + i*sizeof(double));
        phraseAsDoubles[i] = *addressAsDouble;

        std::cout << phraseAsDoubles[i] << std::endl;
    }

    return 0;
}


The above prints the following. These are the same numbers in the original array:

4.2232e+257
2.68904e+161
6.20198e+223
1.23988e+224
1.36702e+161
2.25033e-307
Last edited on Sep 18, 2011 at 8:40pm
Topic archived. No new replies allowed.