Sprintf and hex dumps

Thread Solved

Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Sprintf and hex dumps

 
0
  #1
Jun 20th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sprintf and hex dumps

 
0
  #2
Jun 20th, 2008
"%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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Re: Sprintf and hex dumps

 
0
  #3
Jun 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,849
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 299
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Sprintf and hex dumps

 
0
  #4
Jun 20th, 2008
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 niek_e; Jun 20th, 2008 at 8:40 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sprintf and hex dumps

 
0
  #5
Jun 20th, 2008
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. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,849
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 299
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Sprintf and hex dumps

 
0
  #6
Jun 20th, 2008
@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 niek_e; Jun 20th, 2008 at 8:43 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sprintf and hex dumps

 
0
  #7
Jun 20th, 2008
Originally Posted by niek_e View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,849
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 299
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Sprintf and hex dumps

 
0
  #8
Jun 20th, 2008
Originally Posted by Ancient Dragon View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 21
Reputation: gargg321 is an unknown quantity at this point 
Solved Threads: 0
gargg321 gargg321 is offline Offline
Newbie Poster

Re: Sprintf and hex dumps

 
0
  #9
Jun 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,406
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sprintf and hex dumps

 
0
  #10
Jun 20th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC