CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2009
    Posts
    29

    Have some question about files

    1. How would I go about opening a file as hex?
    2. How would I go about reading parts of the file as hex?
    3. How would I go about editing parts of the file as hex?
    4. How would I go about saving the file as hex?

    Examples:

    1. Open the file and not have things like a byte of 00 translate into a space and then when it is saved as a space to hex, it becomes the byte 20.

    2. Reading the byte at a location other than the beginning of the file, like reading the byte at 0x1F.

    3. Editing the byte at 0x2C when something in the program happens.

    4. Saving the file as hex so that bytes like 00 do not become 20.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Have some question about files

    Strictly speaking, this makes no sense A file is a file, it's just a bunch of bytes. You can consider them as binary, hex, octal, whatever. It doesn't matter - they're still the exact same thing.

    2. Reading the byte at a location other than the beginning of the file, like reading the byte at 0x1F.
    That's what Stream.Seek is for. 0x1F is just an offset, so seek to that offset.

    Saving the file as hex so that bytes like 00 do not become 20.
    I don't know anything that automatically does that kind of conversion.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Nov 2009
    Posts
    29

    Re: Have some question about files

    Quote Originally Posted by Mutant_Fruit View Post
    Strictly speaking, this makes no sense A file is a file, it's just a bunch of bytes. You can consider them as binary, hex, octal, whatever. It doesn't matter - they're still the exact same thing.


    That's what Stream.Seek is for. 0x1F is just an offset, so seek to that offset.


    I don't know anything that automatically does that kind of conversion.
    I been googleing around on how to open and save files with C#, but they are all about txt files.

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Have some question about files

    Quote Originally Posted by KazoWAR View Post
    I been googleing around on how to open and save files with C#, but they are all about txt files.
    oh, really? what google did you use? you must have been searching for it in some parallel reality
    http://www.google.com/search?hl=en&s...=f&oq=&aqi=g10
    and the first result is
    http://www.csharp-examples.net/filestream-open-file/
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    Nov 2009
    Posts
    29

    Re: Have some question about files

    Quote Originally Posted by memeloo View Post
    oh, really? what google did you use? you must have been searching for it in some parallel reality
    http://www.google.com/search?hl=en&s...=f&oq=&aqi=g10
    and the first result is
    http://www.csharp-examples.net/filestream-open-file/
    the google search results:

    FileStream Open File [C#] - Examples are with text files.

    C# Station: Reading and Writing Text Files - Has text files in the title

    Working with Files in C# — CodeGuru.com - Examples are text files

    File.Open Method (String, FileMode) (System.IO) - more text.

    File.Open Method - text

    Visual C# .NET - Open File dialogue box - jpeg?


    list goes on, any ways, when ever it gets to the point about editing and saving a file its always about saving a string to a text file. I do not need that.

    I guess it would be better to start off with txt files, but I cant really think of any kind of program to make that would use that and be of any use to me at all.

    I have starting to study on my own, I was in a class and haven't really done anything out side of it. I want to learn more. Here is a little bit of information of what I am trying to do.

    I am trying to make a program that allows you to open a file. when the file is opened, it reads data at certain points in the file. And displays information based on the data. Here is an example file in hex.

    Code:
       x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF
    
    0x FA 1D 67 53 00 00 2E 91 7C 01 EA 00 AA 76 30 38
    
    1x F2 8F 03 00 DD 1A 00 01 80 00 02 FC 80 00 00 00
    
    2x 00 00 00 00 00 00 00 00 96 01 ED 00 5B 01 69 00
    
    3x 10 18 14 10 03 03 00 03 DF 7C EF 3F 00 00 00 00
    
    4x 02 00 00 00 00 00 9A 00 9A 00 77 00 54 00 53 00
    
    5x 6A 00 FF FF 00 00 00 00 00 00 00 00 00 00 00 07
    
    6x 00 00 00 00 00 00 00 00 B6 00 C6 00 DF 00 D4 00
    
    7x FF FF 00 00 00 00 00 00 00 00 00 09 0A 18 00 00
    
    8x BA 0B 00 0C 23 02 0C 00
    as text...

    Code:
    úgS  .‘|ê ªv08ò Ý € ü€           –* [i  ß|ï?         š š w T S j ÿÿ                   ¶ Æ ß Ô ÿÿ         	
      º #
    as you can see complete gibberish and no way usable, and upon saving the file, all values of 0x00 are saved as 0x20.

    I need to be able to read the values like, the word at 0x50, or the word at 0x38, and by word, I mean a 4 sets of 1 bit vaules. in the example file, the word at 0x38 is 3FEF7CDF. then based on the data have options set. then the user can edit those options, and the data is changed to show that.

  6. #6
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Have some question about files

    then read about binary-read of files and not about text-files. http://www.yoda.arachsys.com/csharp/readbinary.html
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    Re: Have some question about files

    BinaryReader reader = new BinaryReader (File.OpenRead ("path/to/my/file.whatever"));

    Then just read your bytes or words as required. A file is just a load of 1's and 0's. It's not text, it's not hex, it's not octal. If you want to treat those 1's and 0's as hex, then feel free to do so - but that won't change the fundamental order of the 1's and 0's. It's just an alternative way to view those 1's and 0's.

    as you can see complete gibberish and no way usable, and upon saving the file, all values of 0x00 are saved as 0x20.
    You can't convert every arbitrary byte sequence to a valid UTF8 string - which is probably what you've tried to do. When you try to convert a sequence of bytes to a string and that sequence contains bytes which cannot directly be converted to a char, then (usually) they are silently discarded/replaced. This happens for all text encodings.

    The result of this is that if you go bytes -> string -> bytes, then the final byte sequence will be different to what you started off with. This is probably what's happened. The fix? Don't convert bytes to a string unless those bytes are definitely supposed to be a string
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured