C++ question- how can I transform a double or float into a char array?

Mizugori

[H]ard|Gawd
Joined
Mar 25, 2004
Messages
1,240
I need to take a number stored in a variable, and somehow get it into a char array... ie say I have 55701, I need to get it into a char array so the elements are 5, 5, 7, 0, and 1. I have tried all kinds of typecasting and such but I just can't seem to make it work... is there any way to do this? It seems like it must be possible using some kind of loop or something to convert it but I just can't seem to wrap my brain around it...

thanks!
 
(Did you really mean a double or float?)
This might give you a hint:

Code:
#include <iostream>
using namespace std;

int main() {
   int num = 55701;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
}
 
seems like sprintf() should do the trick

That's the way I'd do it, if C functions are allowed/available. If not, I'd use an ostringstream.

Code:
#include <iostream>
using namespace std;

int main() {
   int num = 55701;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
   num /= 10;
   cout << (num % 10) << "\n";
}

I see what you're hinting at, but there are much simpler ways to go about it.
 
Typecasting doesn't work because the conversion involves a new representation, not just changing types. You should study the type system more so that you understand this, because your expectation indicates you're not comfortable with this very fundamental concept.

sprintf() will work, but it's slow. It might be all that's available, depending on what platform you're using. You might have _ecvt, depending on where you're running.
 
I see what you're hinting at, but there are much simpler ways to go about it.

Yeh, I was assuming it was an assignment where it had to be done manually like that and only with a char array.

If it's just to get the result with whatever, I like the ostringstream way. It might not be fast, but that might not be a problem.

Code:
#include <iostream>
#include <string>
#include <sstream>
#include <limits>
using namespace std;

template <typename SrcType>
string str(const SrcType& val) {
    ostringstream s;
    s.precision(std::numeric_limits<SrcType>::digits10 + 1);
    s << val;
    return s.str();
}

int main() {
    cout << str(55701)[2] << "\n";
    cout << str(-1257.238)[6] << "\n";
}
 
ostringstream is fat and slow. Controlling the format is annoying, and its use requires exception handling, and ...
 
Yeh, I was assuming it was an assignment where it had to be done manually like that and only with a char array.

That's true. It would help if Mizugori would tell us more - like platform, is C allowed, etc.

BTW, I like this trick to limit the string size:

Code:
s.precision(std::numeric_limits<SrcType>::digits10 + 1);

I had forgotten about numeric_limits.
 
For C++ the string library is usually used instead a char array, something like this should work:

Code:
#include <string>
#include <sstream>

int main()
{
     int i = 100;
     std::ostringstream oss << i;
     std::string str(oss.str());

     return 0;
}
 
For C++ the string library is usually used instead a char array, something like this should work:
Note that your code converts an integer, not a double. It doesn't compile, anyway. Here's a fixed version:

Code:
#include <string>
#include <sstream>

int main()
{
     double i = 355.0 / 113.0;
     std::ostringstream oss;
     oss << i;
     std::string str(oss.str());

     return 0;
}

The code to use _ecvt() looks like this:

Code:
#include <stdlib.h>
#include <string.h>

int main()
{
     char sz[35];
     double i = 355.0 / 113.0;
     int nDecimal;
     int nSign;
     char *pBuffer = _ecvt( i, 10, &nDecimal, &nSign );
     strcpy( sz, pBuffer );

     return 0;
}

Using Visual Studio 2005, compiling the code that uses ostrstring results in an executable of more than 135,000 bytes. The ecvt version is about 57,000 bytes.

Any guesses about which one is faster?
 
Using Visual Studio 2005, compiling the code that uses ostrstring results in an executable of more than 135,000 bytes. The ecvt version is about 57,000 bytes.

For comparison, with Mingw 4.2.1-dw2 (since touching anything with streams bloats the exe), it's 539KB and 5KB respectively when using "g++ -Wall -Wextra file.cc -o file -O3 -s". Even if you compress the exe with "upx --best", it's 160KB and 3KB.

If the exe is not stripped and not compressed, it's .99MB and 16.4KB. With debug info, it's 1.05MB and 18.2KB.
 
For comparison, with Mingw 4.2.1-dw2 (since touching anything with streams bloats the exe), it's 539KB and 5KB respectively when using "g++ -Wall -Wextra file.cc -o file -O3 -s". Even if you compress the exe with "upx --best", it's 160KB and 3KB.

If the exe is not stripped and not compressed, it's .99MB and 16.4KB. With debug info, it's 1.05MB and 18.2KB.

Another couple of data points just for fun:

With the ancient MinGW 3.4.2 and your g++ line:

249,344 bytes w/ stringstream
5,632 bytes w/ win32-specific call

with -g instead of -s:

567,991 bytes w/ stringstream
19,458 bytes w/ win32-specific call
 
Back
Top