Thread: Print hexadecimal strings from binary

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    30

    Print hexadecimal strings from binary

    Hello to all in forum,

    I'm newbie in C/C++, I would like to have some help from you experts.

    May you give some example in how to search in C or C++ a hexadecimal patterns within a binary file.

    For example:
    String1:
    Code:
    0x44 0x65 0x07
    Then, once found, extract those 3 bytes and the next 11 bytes (14 bytes in total).
    Finally, from those 14 bytes, print without spaces "bytes 1 to 6", "bytes 6 to 12", and byte "13 to 14".
    Then I would like to print last 2 bytes (13 to 14) joined in decimal format.

    The output without any conversion would be:
    Code:
    446507c90688 888000800005 0015
    4465072ec918 059173495269 002C
    44650700cc01 01811bc90b00 00AB
    But the output converting to decimal the last 2 bytes would be:
    Code:
    446507c90688 888000800005 21
    4465072ec918 059173495269 44
    44650700cc01 01811bc90b00 171
    The sample file is attached, and looks like this:
    Code:
    06    00    00    80    00    00    00    80    09    3c    c9    06    88    88    80    00
    80    00    44    65    07    c9    06    88    88    80    00    80    00    05    00    15
    37    06    01    00    00    01    00    65    00    00    00    02    00    00    02    00
    18    00    00    00    03    00    00    03    00    17    00    00    00    04    00    00
    04    00    01    00    00    00    05    00    00    05    00    15    00    00    00    0a
    00    ff    ff    00    65    00    00    44    65    07    2e    c9    18    05    91    73
    49    52    69    00    2C    ff    ff    ff    00    cb    41    04    72    23    01    ff
    ff    ff    00    01    03    ca    03    08    08    fe    cb    0a    00    00    00    00
    00    44    65    07    00    cc    01    01    81    1b    c9    0b    00    00    AB    55
    01    11    04    b1    7f    ff    ff    ff    ca    06    00    00    00    00    00    00
    Thanks for any help or suggestion.
    Attached Files Attached Files
    Last edited by Mestertik; 08-19-2013 at 05:56 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your hex-dump program has a bug. The hex view of your file that you posted in code tags is not an exact representation of the file you attached. For example, starting at line 8 in the code tags, your actual file does not have two FF's in a row.

    At any rate, you should post your attempt at solving your problem.

    (And your binary file should probably have an extension other than .txt since it's definitely not a text file. You can give it no extension, or .bin, or one you invent.)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello oogabooga,

    I'm asking for some help in this because I don't know how to begin to make an attempt. I have the idea to use regular expression but I don't know how to apply it and if it is a good option.

    I'm attaching again the file and now I've verified the hexdump is correct representation of code tags.

    PD: I've put "txt" extension because without extension or using another extension the forum application doesn't let me upload the file.

    Code:
    06 00 00 80 00 00 00 80  09 3c c9 06 88 88 80 00
    80 00 44 65 07 c9 06 88  88 80 00 80 00 05 00 15
    37 06 01 00 00 01 00 65  00 00 00 02 00 00 02 00
    18 00 00 00 03 00 00 03  00 17 00 00 00 04 00 00
    04 00 01 00 00 00 05 00  00 05 00 15 00 00 00 0a
    00 ff ff 00 65 00 00 44  65 07 2e c9 18 05 91 73
    49 52 69 00 2c ff ff ff  00 cb 41 04 72 23 01 ff
    ff 00 01 03 ca 03 08 08  fe cb 0a 00 00 00 00 00
    44 65 07 00 cc 01 01 81  1b c9 0b 00 00 ab 55 33
    01 11 04 b1 7f ff ff ca  06 00 00 00 00 00 00 00
    Thanks for any help.
    Attached Files Attached Files
    Last edited by Mestertik; 08-19-2013 at 08:26 PM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    As a first attempt, try creating a program that reads the file a byte at a time and when it reads a 0x44 byte, prints out that byte and the next 13 bytes. Post the program.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Input iterators may be helpful.
    Other than that, there are a lot of algorithms that work with iterations and there are also output formatting for writing to file/console (ostream).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello oogabooga,

    I'm trying to do what you say, still trying.

    Hello Elysia,

    May you help me with some sample of iterator you say applied to my question.

    Thanks in advance for any help.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Have you tried? If so, post your code.
    Make a small program from scratch that applies these techniques to practice them. Once you've got that down, you can apply it to your big problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    30
    Hello Elysia,

    I actually posted requesting for some help to give me some example in how to do this
    to have a point of start because I almost don't know C++ language.

    Best regards

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you don't know the C++ language, then you should probably read a beginner's book (such as this).
    But let's start with you do know. Do you know what iterators are? Do you know what algorithms there are or where to find them? Do you know how to format output?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Hexadecimal Numbers Of a File
    By nathanpc in forum C Programming
    Replies: 4
    Last Post: 01-13-2010, 11:19 PM
  2. Characters to binary... hexadecimal to binary
    By Trauts in forum C++ Programming
    Replies: 48
    Last Post: 10-27-2002, 05:03 PM
  3. Binary/Hexadecimal
    By Trauts in forum C++ Programming
    Replies: 24
    Last Post: 09-13-2002, 11:48 PM
  4. Hexadecimal and Binary
    By Yaj in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-15-2002, 11:41 PM
  5. Binary,Hexadecimal to Text
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-11-2002, 07:55 AM