Is there a function to convert a numeric value into a string?

This is a discussion on Is there a function to convert a numeric value into a string? within the C Programming forums, part of the General Programming Boards category; Hello. I was wondering if there is a standard library function to convert a numeric value into a string. Compiler: ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    79

    Is there a function to convert a numeric value into a string?

    Hello. I was wondering if there is a standard library function to convert a numeric value into a string.

    Compiler: Turbo C++ v1.01

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    98
    sprintf( strVariable, "%d", intVariable );

    will do it, but no doubt someone knows a more efficient way!

  3. #3
    Registered User Master_Rondal's Avatar
    Join Date
    Oct 2002
    Posts
    9
    you could probably use the strcpy();

    but you would have to use the sting.h header file

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    sprintf () would be my choice
    hello, internet!

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Hi!

    I've never heard about such function, but snprintf() could be a good choice. I'm not sure if this code below is working properly, but try it. The integer parsing stuff functional, it will convert any integer to string.

    char *ParseNum(void* num, int flag) {

    #define I 1
    #define D 2


    char *string; int x = (num < 0) ? 1 : 0;
    unsigned int div = 10, count;
    size_t size = sizeof(char);

    switch(flag) {
    case I:
    x = 0, count = (int) num;
    while((count = count / div) > div) { x = x++; } x = x + 1;
    string = (char *) xmalloc(x);
    snprintf(string,(size * x) + 1,"%d",(int) num);
    memset(&string[x+1],'\0',size); break;
    case D:
    string = (char *) xmalloc(sizeof(double)+1);
    snprintf(string,(sizeof(char) * sizeof(double)) + 1,"1.5Lf",num);
    break;
    }

    return string;
    }


    Hope this will be useful to you!

    Bye!
    Best Regards,

    Bill

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    30
    Just a comment:

    The void* arg is not good, I've used for testing something. Use double instead.
    Best Regards,

    Bill

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Just a comment:
    .. and two more comments:
    - use code tags when posting code;
    - don't double post, if you've made a mistake in a post, just edit it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21