943,568 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6212
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 20th, 2008
0

Sprintf and hex dumps

Expand Post »
Hey Guys,

I am new to C programming and I got a task of storing hex dumps in character array and then use them as a string. I use the sprintf function for the job but it is giving me incorrect results. Please help me out. I am writing a prototype of the functionality I want to use.

  1. #include<stdio.h>
  2. int main()
  3. {
  4. char a[25];
  5. int i;
  6. sprintf(a,"%d%d%d",0x0000001F,0x00000001,0x00271418);
  7. for(i=0;i<8;i++)
  8. {
  9. printf("\n%d",a[i]);
  10. }
  11. return 0;
  12. }
Last edited by Ancient Dragon; Jun 20th, 2008 at 8:13 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jun 20th, 2008
0

Re: Sprintf and hex dumps

"%d" will produce decimal. If you want hex then use "%x"

Try this:
  1. int main()
  2. {
  3. char a[25];
  4. int i;
  5. sprintf(a,"%x%x%x",0x0000001F,0x00000001,0x00271418);
  6. for(i=0;i<8;i++)
  7. {
  8. printf("%c\n",a[i]);
  9. }
  10. return 0;
  11. }
Last edited by Ancient Dragon; Jun 20th, 2008 at 8:20 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 20th, 2008
0

Re: Sprintf and hex dumps

I have tried it already. 1F should return the value 31 but instead it is returning 33. Same with all hex dumps. they all are returning incorrect results.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jun 20th, 2008
0

Re: Sprintf and hex dumps

Change the %x to %d with the sprintf()
use %c when printing the chars

this code:
  1. int main()
  2. {
  3. char a[25];
  4. int i;
  5. sprintf(a,"%d%d%d",0x0000001F,0x00000001,0x00271418);
  6. for(i=0;i<8;i++)
  7. {
  8. printf("%c",a[i]);
  9. }
  10. return 0;
  11. }

works fine for me..
Last edited by Nick Evan; Jun 20th, 2008 at 8:40 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 20th, 2008
0

Re: Sprintf and hex dumps

sprintf() doesn't work that way. Each digit of the number will be placed in a different element of the character array. So for 0x1f the a[0] == '1' and a[1] == 'f' (assuming "%x")

  1. #include<stdio.h>
  2. int main()
  3. {
  4. char a[25] = {0};
  5. int i;
  6. a[0] = 0x1f;
  7. a[1] = 0x01;
  8. a[2] = 0x00271418;
  9. //sprintf(a,"%4d%4d%4d",0x0000001F,0x00000001,0x00271418);
  10. for(i=0; i <3; i++)
  11. {
  12. printf("%d\n",a[i]);
  13. }
  14. return 0;
  15. }
  16. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 20th, 2008
0

Re: Sprintf and hex dumps

@AD:

But that's not what he wants right? I thought he wanted the decimal values of the hex numbers stored as a string in a char-array?
So my code should be OK
Last edited by Nick Evan; Jun 20th, 2008 at 8:43 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 20th, 2008
0

Re: Sprintf and hex dumps

Click to Expand / Collapse  Quote originally posted by niek_e ...
Change the %x to %d with the sprintf()
use %c when printing the chars

this code:

works fine for me..
But I think he was expending a[0] to be 31.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jun 20th, 2008
0

Re: Sprintf and hex dumps

But I think he was expending a[0] to be 31.
Hmm. Let's ask:

@gargg321:

Do you want an array like this:
a[0] = 31
a[1] = 1
etc

or like this:
a[0] = '3'
a[1] = '1'
a[2] = '1'
etc
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 20th, 2008
0

Re: Sprintf and hex dumps

Guys I like the array to be of first option. I think the solution given by Ancient Dragon is OK with me but he is not using the sprintf function, instead he's given every element of array indidually. The string of hex dumps I have to use is real long and giving individual values is simply not possible. When I am using sprintf the problem still persists.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gargg321 is offline Offline
21 posts
since Jun 2008
Jun 20th, 2008
0

Re: Sprintf and hex dumps

You didn't answer Niek's question. Which way do you expect to see the data ? If you want it similiar to what I posted then you can't use sprintf(). And the way I posted, one char can only hold a value up to 127 decimal (signed integer). If the values are larger than that then you can't do it my way.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to use timer in C?
Next Thread in C Forum Timeline: Help in taking input





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC