Results 1 to 6 of 6

Thread: Converting numbers to Strings

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    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?

  2. #2
    Addicted Member
    Join Date
    Oct 1999
    Location
    Dallas,TX
    Posts
    170
    #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

  3. #3
    Addicted Member
    Join Date
    Oct 1999
    Location
    Dallas,TX
    Posts
    170
    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

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    ...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

  5. #5
    Lively Member
    Join Date
    Sep 2000
    Location
    Singapore
    Posts
    78

    Smile

    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.

  6. #6
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Code:
    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:
    Code:
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width