#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Using of sprintf


    I am using sprintf to copy from array a to array b. I get the output as
    a: 01 02
    b: 30 30

    Why are a and b different?
    Or am I missing something?
    Please help.


    #include <stdio.h>
    void main()
    {
    unsigned char a[2]={0x1, 0x2};
    unsigned char b[2]={0};
    int i;

    for(i=0; i<2; i++){
    sprintf((b+i), "%02x", *(a+i));
    }
    printf("\na:");
    for(i=0; i<2; i++){
    printf("\t %02x\t", *(a+i));
    }
    printf("\nb:");
    for(i=0; i<2; i++){
    printf("\t %02x\t", *(b+i));
    }

    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    read this before you post more code

    > Why are a and b different?
    Probably because you only have a[2] and b[2], and you're overflowing your buffers.

    "%02X" will write 3 chars into your buffer.

    > sprintf((b+i), "%02x", *(a+i));
    Since i only increments, and each sprintf produces 2 chars, then you're also overwriting previous output.

    Comments on this post

    • yedapoda agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    First, sprintf is not typically used to copy data between arrays; it is used to create formatted strings from data, which is what you are seeing.

    0x30 = 48 (decimal), the ASCII code for '0'. The first sprintf writes the characters '0' (0x30), '1' (0x31), and '\0' (0x00). Note: this is already a buffer overflow, since your array only has size 2. The second sprintf writes the characters '0' (0x30), '2' (0x32), and '\0' (0x00) to the array offset by one. Again, this is a buffer overflow.

    The end result is:

    b[0] = '0' = 0x30
    b[1] = '0' = 0x30
    b[2] = '2' = 0x32 (overflow)
    b[3] = '\0' = 0x00 (overflow)

    Comments on this post

    • yedapoda agrees
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Keeping all the other replies in mind, you have mis-declared b. a contains two bytes of binary data, whereas you want b to contain two strings of character (ASCII) data. That is what you use sprintf for, to convert binary data into strings.

    The declarations should instead be (please note the use of code tags, which you must also learn to use when posting code):
    Code:
        unsigned char a[2]={0x1, 0x2};
        unsigned char b[2][3] = {"", ""};
    Then you need to correct the rest of your program accordingly (plus a few minor corrections):
    Code:
    #include <stdio.h>
    
    int main()
    {
        unsigned char a[2]={0x1, 0x2};
        unsigned char b[2][3]={"",""};
        int i;
    
        for(i=0; i<2; i++)
        {
            sprintf(*(b+i), "%02x", *(a+i));
        }
        printf("\na:");
    
        for(i=0; i<2; i++)
        {
            printf("\t %02x\t", *(a+i));
        }
        printf("\nb:");
    
        for(i=0; i<2; i++)
        {
            printf("\t %s\t", *(b+i));
        }
    
        return 0;
    }
    Which yields this output:
    a: 01 02
    b: 01 02
    Of course, if you did not want to create string representations of your binary data but rather wanted to copy the values from a to b, then you should not be using sprintf but rather simple assignment statements.

    Comments on this post

    • yedapoda agrees
    Last edited by dwise1_aol; October 9th, 2012 at 02:25 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Thanks a lot for the information.

IMN logo majestic logo threadwatch logo seochat tools logo