|
-
Sep 30th, 2000, 10:31 AM
#1
Thread Starter
Frenzied Member
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?
-
Sep 30th, 2000, 11:19 AM
#2
Addicted Member
#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
-
Sep 30th, 2000, 12:19 PM
#3
Addicted Member
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
-
Sep 30th, 2000, 12:23 PM
#4
Monday Morning Lunatic
...although technically, DOS strings end with either Ctrl-Z or "$". (Just to be randomly confusing )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 30th, 2000, 01:05 PM
#5
Lively Member
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.
-
Oct 2nd, 2000, 08:57 AM
#6
Guru
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|