PDA

Click to See Complete Forum and Search --> : Converting numbers to Strings


Sam Finch
Sep 30th, 2000, 10:31 AM
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


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?

PhilipG
Sep 30th, 2000, 11:19 AM
#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

PhilipG
Sep 30th, 2000, 12:19 PM
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

parksie
Sep 30th, 2000, 12:23 PM
...although technically, DOS strings end with either Ctrl-Z or "$". (Just to be randomly confusing :()

Blitz
Sep 30th, 2000, 01:05 PM
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.

Yonatan
Oct 2nd, 2000, 08:57 AM
char *s1 = 0;

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:

char s1[80];

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: