| | |
Sprintf and hex dumps
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 21
Reputation:
Solved Threads: 0
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.
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.
c Syntax (Toggle Plain Text)
#include<stdio.h> int main() { char a[25]; int i; sprintf(a,"%d%d%d",0x0000001F,0x00000001,0x00271418); for(i=0;i<8;i++) { printf("\n%d",a[i]); } return 0; }
Last edited by Ancient Dragon; Jun 20th, 2008 at 8:13 am. Reason: add code tags
"%d" will produce decimal. If you want hex then use "%x"
Try this:
Try this:
C Syntax (Toggle Plain Text)
int main() { char a[25]; int i; sprintf(a,"%x%x%x",0x0000001F,0x00000001,0x00271418); for(i=0;i<8;i++) { printf("%c\n",a[i]); } return 0; }
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.
Change the %x to %d with the sprintf()
use %c when printing the chars
this code:
works fine for me..
use %c when printing the chars
this code:
c Syntax (Toggle Plain Text)
int main() { char a[25]; int i; sprintf(a,"%d%d%d",0x0000001F,0x00000001,0x00271418); for(i=0;i<8;i++) { printf("%c",a[i]); } return 0; }
works fine for me..
Last edited by niek_e; Jun 20th, 2008 at 8:40 am.
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")
C Syntax (Toggle Plain Text)
#include<stdio.h> int main() { char a[25] = {0}; int i; a[0] = 0x1f; a[1] = 0x01; a[2] = 0x00271418; //sprintf(a,"%4d%4d%4d",0x0000001F,0x00000001,0x00271418); for(i=0; i <3; i++) { printf("%d\n",a[i]); } return 0; } }
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.
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.
•
•
Join Date: Jun 2008
Posts: 21
Reputation:
Solved Threads: 0
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.
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.
![]() |
Other Threads in the C Forum
- Previous Thread: How to use timer in C?
- Next Thread: Help in taking input
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






