-
how do I Convert a number into a BSTR? or just a string? (a BSRT is a visual basic string)
I need the C++ equivilent of
Code:
Dim str As String
Dim x As Double
x = 4
str = CStr(x)
I've tried VarBstrFromR8 API but it gives me the hexidecimal string, how do I get the actual string?
-
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
void main()
{
int sign, decimal_position;
int number_of_decimal_digits = 7;
char str[20];
double x;
x = 4.123456789;
strcpy(str, _fcvt(x, number_of_decimal_digits, &decimal_position, &sign));
cout << str;
}
Just remember that _fcvt does not format the conversion with the decimal point in the string. Since you know the decimal_position, finishing off the double conversion should be easy.
Phil
-
Oh yeah, and did you mention what type of string you would be working with? I assumed you ment good ol' dos\C strings. If your working with VC++ and the CString object, conversion will be a piece of cake!
Phil
-
...although technically, DOS strings end with either Ctrl-Z or "$". (Just to be randomly confusing :()
-
You can try using sprintf()
If I'm didn't remember wrongly in the syntax, it should be:
int num = 50;
char *s1 = 0;
sprintf(s1, "%d", num);
That should do it.
-
This is wrong, because it turns the pointer s1 into a null pointer.
It must point at a valid value.
You can change it to:
Then, it points at an array of 80 chars.
You can also use the old itoa, ftoa, etc. functions, but sprintf is more fun! :rolleyes: