Thread: integer array to string

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    integer array to string

    I have an array of integers (binary[4]) and I want to put the contents into a string i.e
    binary[0] = 1
    binary[1] = 0
    binary[2] = 1
    binary[3] = 1
    and i want a string
    str = 1011
    I thought of doing this
    Code:
    char str[50] //buffer size so never overflow
    sprintf( str, "%d%d%d%d", binary[0],binary[1],binary[2],binary[3] );
    But I dont always know the size of binary and anymore than 4 and i dont want to have to type it in How can I do it in a loop because
    Code:
    for i = 0;i<5;i++
    {
    sprintf( str, "%d", binary[i]);
    }
    just overwrites it

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Look at strcat

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Or, since you know each number will be exactly one digit, you can sprintf into the middle of the string:
    Code:
    for (i = 0; i < 5; i++) {
        sprintf(&str[i], "%d", binary[i]);
    }
    Each time through the loop, you will write one digit, followed by a null. You will overwrite the previous null terminator with each iteration, but the final one will remain, leaving you with a proper string.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Initialize str[] to NULL, then supply it as an argument to sprintf():
    Code:
    for (i = 0; i < 4; i++)
        sprintf(str, "%s%d", str, binary[i]);

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by itCbitC View Post
    Initialize str[] to NULL, then supply it as an argument to sprintf():
    Code:
    for (i = 0; i < 4; i++)
        sprintf(str, "%s%d", str, binary[i]);
    Not such a good idea:
    Quote Originally Posted by man sprintf
    NOTES
    Some programs imprudently rely on code such as the following


    sprintf(buf, "%s some further text", buf);


    to append text to buf. However, the standards explicitly note that the results are undefined if source and destination buffers overlap when calling sprintf(), snprintf(),
    vsprintf(), and vsnprintf().

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by anduril462 View Post
    Not such a good idea:
    Gotcha!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-07-2011, 04:54 PM
  2. Array having Integer and String elements
    By skm in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2004, 03:44 AM
  3. char array string > integer
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-03-2004, 11:28 AM
  4. Converting a "Char" string to an Integer Array
    By Jazz in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2001, 01:50 PM
  5. String To Integer Array!!!!
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-29-2001, 10:15 AM