Read binary, convert to hex

Ok, i have an executable that i want to be able to edit the hex values of, but i can't find anything that will read the hex from the file...
I was wondering if it might work if i did a regular BinaryRead
and just converted whatever it read from binary to hex, then edited that and converted it back to binary and do a BinaryWrite. Would that work the same as if i had directly edited the hex

I'm pretty new to this, but im just trying to basically end up with

1. Read the hex of the file.
2. Change it according to what the user enters.
3. Write it back to the file.


thanks a lot!


Answer this question

Read binary, convert to hex

  • CharissaJB123

    Ok, thanks alot so far, you've answered my main question, and i've gotten alot further. I've gotten to the point where i've read the bytes i've wanted and converted to hex, but my last question is

    When I change the format to hex, the 0's come out as just one 0...
    I.E.

    should be: 00 2F 00 32 28 00 01 7F 02 7F 04 7F
    but instead its: 0 2F 0 32 28 0 1 7F 2 7F 4 7F FF

    shouldnt be that hard, thanks again for the help

  • Mac Jamb

    You're looking for b.ToString("X2").

  • JoshKorn

    The bytes are just numbers.  You can treat them as 8 bits (binary digits), two hexadecimal digits, or a decimal value from 0 to 255.  The computer couldn't really care less (well, at least from the viewpoint of a c# program).  If you just do something like
        byte b = readMyNextByte();
        Console.WriteLine(b.ToString());
    then it will end up showing it to you in decimal form, but that's just because decimal is what the library designers decided would be most common.  If you would rather get the hexadecimal representation, use b.ToString("x") instead.  As far as I know, there isn't a built-in way to display binary, but it's not hard to do it if you understand what binary is.

    The reason why such programs as the one you're writing are usually called hex editors is because, as I said, a byte is always exactly two hexadecimal digits (allowing zeros, of course).  You could call it a "decimal editor" and the data would look like 112, 4, 56, 0, 0, 241, 1, 56, 88, 129 instead of A3, 7D, 03, 00, 00, 08, 10, C1 (these aren't equal, as you can tell).


  • Suthy67

    kk, so then once i read the bytes, is it in binary do i convert it to hex from binary thanks

  • Jarek Błaszczyk

    !!! Thank you sooooo much, your a genius man !
    Works great and im good to go!
    thanks again!

  • Ignatius V Ignatius

    The BinaryReader/Writer classes are for loading and saving C# data, not for directly accessing the file. What you're looking for is the FileStream class. Use its Read and Write or ReadByte and WriteByte methods to access the individual bytes.

  • Read binary, convert to hex