The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Using of sprintf
Discuss Using of sprintf in the C Programming forum on Dev Shed. Using of sprintf C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 9th, 2012, 11:50 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
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));
}
}
|

October 9th, 2012, 12:06 PM
|
 |
Contributed User
|
|
|
|
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.
|

October 9th, 2012, 12:10 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
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)
|

October 9th, 2012, 01:56 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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:
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.
Last edited by dwise1_aol : October 9th, 2012 at 02:25 PM.
|

October 11th, 2012, 02:37 AM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 4
Time spent in forums: 33 m 55 sec
Reputation Power: 0
|
|
|
Thanks a lot for the information.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|