Simple casting problem

This is a discussion on Simple casting problem within the C Programming forums, part of the General Programming Boards category; I bet this question has come up more then once, I searched but I didn't find any thing that worked? ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    47

    Simple casting problem

    I bet this question has come up more then once, I searched but I didn't find any thing that worked?

    how to convert an int to a char[]

    Code:
    int a = 10;
    char buf[100];
    
    buf = (char)a; // wont work...
    
    
    char *buf2;
    
    buf2 = (char *)a; // wont work either
    so how do I do it?

  2. #2
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    There's also itoa.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    thanks, sprintf works, didn't get itoa to work.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Code:
    int my_num = 1000;
    char my_buf[10];
    _itoa(my_num, my_buf, 10);
    Or
    Code:
    _itoa_s(my_num, my_buf, 10, 10);

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by micke_b View Post
    didn't get itoa to work.
    That's good. Use sprintf() if want to write portable code. itoa() may work in some C compilers, but sprintf() will work in any C compiler.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I doubt very much that micke_b will have either of _itoa or _itoa_s since both are MS special naming (the latter only in the latest two or so versions of VS C++).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    That's weird, because the doc says
    Use the ISO C++ conformant _itoa or security-enhanced _itoa_s instead
    So either something is wrong or it's lying.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, I haven't been able to find a non-Windows version in my 30 seconds or so of googling.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    Code:
    int my_num = 1000;
    char my_buf[10];
    _itoa(my_num, my_buf, 10);
    gives: undefined reference to `_itoa'

    Code:
    _itoa_s(my_num, my_buf, 10, 10);
    gives: undefined reference to `_itoa_s'


    no man pages for either.

    running gcc 4.2.1

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    If you want to give it a try, it's included in <stdlib.h> according to doc. It works on VC++, but I dunno about GCC.
    As as stated, I don't know if it works on non-windows.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    Had stdlib.h included... so I guess I just use sprintf

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Sure, there's no real advantage on using itoa anyway.

  14. #14
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    maybe interesting: (from "K&R C" )
    Code:
    void reverse(char s[])
    {
            int c,i, j;
            for (i=0,j= strlen(s)-1; i<j ;i++,j--){
                    c= s[i];
                    s[i]=s[j];
                    s[j]=c;
            }
    }
    
    void itoa(int n, char s[])
    {
            int i, sign;
            if ((sign=n)<0)
                    n=-n;
            i=0;
            do {
                    s[i++]= n &#37;     10 +'0';
            }while((n/=10)>0);
            if(sign<0)
                    s[i++]='-';
            s[i]='\0';
            reverse(s);
    }

  15. #15
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Elysia View Post
    That's weird, because the doc says
    So either something is wrong or it's lying.
    Microsoft's documentation is riddled with lies. Most are accidental, some seem pretty damn intentional sometimes.

    I have no idea if itoa() is a standard function in C++. I didn't think it was. At any rate, this is C, not C++.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fairly simple problem
    By fatdunky in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 10:34 PM
  2. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 03:07 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  5. Simple? winsock client - server problem
    By knutso in forum Windows Programming
    Replies: 2
    Last Post: 03-26-2003, 03:51 AM

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