C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 9th, 2012, 11:50 AM
yedapoda yedapoda is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 yedapoda User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 55 sec
Reputation 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));
}

}

Reply With Quote
  #2  
Old October 9th, 2012, 12:06 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 51 m 23 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old October 9th, 2012, 12:10 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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!

Reply With Quote
  #4  
Old October 9th, 2012, 01:56 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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:
Quote:
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.

Reply With Quote
  #5  
Old October 11th, 2012, 02:37 AM
yedapoda yedapoda is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 4 yedapoda User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 55 sec
Reputation Power: 0
Thanks a lot for the information.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Using of sprintf

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap