Examine hex number?

This is a discussion on Examine hex number? within the C Programming forums, part of the General Programming Boards category; If I read from a file a %0.2X number, and I bring in a large number of these, how would ...

  1. #1
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65

    Examine hex number?

    If I read from a file a %0.2X number, and I bring in a large number of these, how would I examine each byte with if?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That depends what kind of datatype you (need to) use. Do you want to break these up into ints or something? You could just put the whole file into one big char buffer and parse it with read(), which has no problem with zero bytes -- although you could do this when you read the file in:
    Code:
    int fd=open("file", O_RDONLY), i++;
    char byte, buffer[4096];
    while ((read(fd,&byte,1))   {    /* true until EOF */
         [filter or look for bytes?]
         buffer[i]=byte; i++;
    }
    just a sketch
    Last edited by MK27; 02-28-2009 at 10:11 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    For example, my program reads F4 CD 21. I examine these

    Code:
    switch(opcode)
    {
        case F4:
            puts("F4        HLT");
            break;
        case CD:
            puts("CD        INT");
            break;
        default:
            printf("%0.2X        ???");
    }
    Also, how would I make it recognize that CD 21 goes together as INT 21?

    And I would use stat() to get the number of bytes and then decrement it to make s0ure I got the whole file.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can use that switch with (byte) in the above read() loop as part of a "parsing pipeline":
    Code:
    int fd=open("file", O_RDONLY), i++;
    char byte, buffer[4096], tmp[256];
    while ((read(fd,&byte,1))   {    /* true until EOF */
         switch (byte) {
               case (0xcd):  read(fd,&byte,1);
                        sprintf(tmp,"INT %d",(int)byte);
                        strcat(buffer,tmp);  break;
               case (0xf4) :      etc....
        
    }
    Last edited by MK27; 02-28-2009 at 10:16 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    How would you do that using fread()

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes, fread() is more portable than read, sorry.

    You do it the same way but you use a FILE* stream -- fopen() -- instead of a file descriptor from open(). The fread() would look like this:
    Code:
    while ((fread(&byte,1,1,fstr))) {
    I just noticed I have some unclosed parantheses in my previous work here, but anyway...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    My only problem is, how would I get it to recognize certian bytes as prefixes rather than ???. And how would I grab only a byte at a time.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sly View Post
    My only problem is, how would I get it to recognize certian bytes as prefixes rather than ???. And how would I grab only a byte at a time.
    Let's look at this more closely!
    Code:
    #include <stdio.h>
    
    int main() {
            int halt=0;
            FILE *file=fopen("file", "r");
            unsigned char byte;
            while ((fread(&byte,1,1,file)))   {    /* true until EOF */
                    switch (byte) {
                            case (0xcd):            /* notice: 1,1 means one byte at a time */
                                    fread(&byte,1,1,file);  
                                    printf("INT %d\n",(int)byte);
                                    break;
                            case (0xf4): 
                                    halt=1;
                                    break;
                            default: break;       /* could use this for something too */
                    }   
                    if (halt==1) break;
                    /* still more opportunities to work with "byte" */
            }   
            return 0;                           
    }
    I just noticed I get a gcc warning unless you use an unsigned char here, because 0xf4 would be an "illegal" value for a char.

    Anyway, you get it to recognize a certain byte as a prefix using the switch statement -- but if you are worried that there will be a 0xcd that is not really a 0xcd, well...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    But if the EOF comes up as an opcode...


    Code:
    #include <stdio.h>
    
    int main() {
            int x;
            struct stat fbuf;
            int halt=0;
            FILE *file=fopen("file", "r");
            unsigned char byte;
            x = stat(argc[1],&fbuf);
            x = fbuf.st_size;
            while ((fread(&byte,1,1,file)))   {    /* true until EOF */
                    switch (byte) {
                            case (0xcd):            /* notice: 1,1 means one byte at a time */
                                    fread(&byte,1,1,file);  
                                    printf("INT %d\n",(int)byte);
                                    break;
                            case (0xf4): 
                                    halt=1;
                                    break;
                            default: break;       /* could use this for something too */
                    }   
                    if (halt==1) break;
                    /* still more opportunities to work with "byte" */
            }   
            return 0;                           
    }
    Where would I put the x--? Also, I don't really understand how to do this with all possible hex opcodes.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why to use fread for reading 1 byte?
    what is the problem with fgetc?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I don't just want to read one byte, I want to grab all of them and examine them as one byte chunks. Also, I need sometimes to examine them as more than one byte.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart View Post
    why to use fread for reading 1 byte?
    what is the problem with fgetc?
    I would say it doesn't matter much, but then fgetc returns an int.

    Also, as the OP indicates, sh/e may in fact need to read more than one byte.

    If you do that, you can use a small buffer and iterate thru each byte in a loop:
    Code:
    char buffer[256];
    fread(buffer,1,256,file);
    for (i=0; i<256; i++) {
            switch (buffer[i]) {
         ...etc.
    Just keep in mind that "buffer" will not be null terminated.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    OK. Thanks. But where would I put the x--?
    Last edited by Sly; 02-28-2009 at 11:11 AM.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
            x = stat(argc[1],&fbuf);
            x = fbuf.st_size;
    stat() is just as portable (or unportable) as open()/read()/etc. If you want to figure out the size of a file, open it, fseek() to the end, ftell() to get the position, and fseek()/rewind() back to the beginning of the file.

    I don't know what you're asking with the "x--" question. If stat() returns the number of bytes in the file, loop in the range 0 to x-1.

    But I don't see why you need to know the size of the file, anyway. MK27's code handles files of any size. Perhaps you need the count for something else, but you haven't said so. In fact, you haven't said what you're doing at all (though I'd guess some sort of disassembler). What are you doing?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I have a disassembler for x86, it's about 36KB in size.

    It is table-driven, and although it say "disasm.cpp", it's not really C++ code.

    It doesn't do the MMX/3DNow!/SSE instructions, or any other new instructions introduced in recent processors (branch prediction prefix for example).

    But you are welcome to use it as a template, or simply play around with it.

    Note that it is NOT an attempt on "most pretty code" - it does use a few different dirty tricks that may not be portable, it has at least one goto (that I spotted when doing a fast scan through the code), and I'm sure there are other things that aren't great either.

    Due to the extension rules, the file is a renamed zip-file.

    Edit: Perhaps I should have stated that I'm happy for anyone to use the source code, but you must mention that I wrote the original code - I think that's fair.

    --
    Mats
    Attached Files Attached Files
    Last edited by matsp; 02-28-2009 at 06:44 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. string to hex
    By LogicError in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2005, 06:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21