how to inspect the bytes in a file?

What is the easiest way to inspect the bytes stored in a file?

Ideally, If my file was 10 bytes each of which had only the high bit set, I'd be able to browse for it and get output like this:

01 - 10000000
02 - 10000000
03 - 10000000
04 - 10000000
05 - 10000000
06 - 10000000
07 - 10000000
08 - 10000000
09 - 10000000
10 - 10000000

or perhaps this:

01 - 80
02 - 80
03 - 80
04 - 80
05 - 80
06 - 80
07 - 80
08 - 80
09 - 80
10 - 80

I'm currently coding on a Mac.

In pseudo-code?

open file
while !eof
    read byte
    if matching
        print counter - byte
    counter++
close file

That I already knew. Translating that pseudo-code into actual C or C++ or Java code is hardly the easiest way to do this. Someone recommended googling 'Hex Editor' which I will be doing now.

Since your original posting sounded (and still does) like "Please do my homework for me" I didn't want to spill too much. So please tell me: are you learning C/C++/Java? By yourself or in class? Is this a homework assignment? What have you got so far? Are you stuck on any specific part?
I'll be happy to help someone learn as long as I'm sure I'm not helping cheat.

I graduated from college years ago. I have since been writing a lot of PHP and have rarely, if ever, dealt with raw binary files. I can totally appreciate your desire to not assist those little cheats. I am not one of them. I am, however, returning to C++ after many years absence.

I'm using hexdump which seems perfect:

My-Mac:red sneakyimp$ hexdump -vx rgb.raw
0000000    0000    0000    0000    0000    0000    0000    0000    0000
0000010    0000    0000    0000    0000    ffff    0000    0000    0000
0000020    0000    0000
0000024

If you only want to display a raw file, hexdump is perfect for that. From your original post, however, it seemed that you only want to display those postition matching a certain pattern of that file.

Should you want to implement something similar yourself, file opening and loops work pretty much the same as in PHP, "strings" are really arrays of char (which is 1 byte), and printf has a %X conversion, which will output the value as hex. If you need more information, feel free to ask.

I think we got confused since you were asking in the programming forum. You just wanted an existing tool, people thought you wanted us to make you one. :slight_smile:

Aha. In retrospect I should perhaps have put this post in 'linux for dummies' forum or perhaps in 'unix and linux applications'. My original thinking was that there *must* be some tool in XCode or typically existing in an IDE for inspecting the bits/hex of a binary file. I have installed Eclipse also but the library I must work with apparently requires the CoreServices library which is so easy to link up in XCode. So far I'm still just getting used to the elaborate IDE stuff. I code PHP in a glorified text editor.

Pludi, that %X tip is a good one for me at this point. I was using %d on floats and %s on integers and getting some pretty weird behavior. Darn strongly typed languages! I'm hoping to learn (re-learn?) all the tips for finding the right library for my task at hand but also at finding the documentation for a given library or command.

Cheers! Thanks for the help. I'll probably be around asking lots more questions -- and hopefully answering a few as payback if I can get my footing.