Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
H

htres

@htres
About
Posts
4
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How to retrieve non fixed-length records from a binary file
    H htres

    Luc Pattyn wrote:

    I would not fully trust the string.IndexOf part, since string operations perform unpredictably on non-string data (such as JPEG images, which can contain any bit pattern, that could be misinterpreted as Unicode characters).

    You are right, I had a lot of trouble with string.IndexOf when I was trying to isolate just the jpeg by searching for small strings of 2-4 chars. But after a lot of testing using it to find the record delimiter, which is the same 16 byte string in every record, it works very reliably. Even though it is entirely possible for this particular 16 byte string to show up within the jpeg encoding, the odds are against it.

    Luc Pattyn wrote:

    htres wrote: each record has a fixed 223 byte header That's new info, makes things easier I guess.

    Yeah it was a lot easier. That 223 byte header contained the fixed length fields that held the information about the image. I probably should have posted an example of the file format...but it would have been ugly since it is mostly binary.

    Luc Pattyn wrote:

    htres wrote: wrote it to disk, named it .jpg, and tada, I had the jpeg image As I mentioned earlier if you want the image I guess you can do it without such file using Image.FromStream(); if you need the file, then it is the way to go.

    I haven't tried the Image.FromStream option yet, though I plan to eventually. Ultimately I'd like to populate a database with the picture and header data as well. But I'll leave that part for a new thread... ;) Thanks for the help!

    C# csharp json tutorial

  • How to retrieve non fixed-length records from a binary file
    H htres

    Well, I was finally able to do this in a pretty efficient way. I just read in the entire file, about 256k, to a byte array. Then I could convert it to a string in order to use string.IndexOf to find the record delimiters. I then used those demlimiter positions and Array.Copy to copy what I wanted out of the original byte[] to it's own byte[]. From there it was easy to get the image because each record has a fixed 223 byte header, so the remainder of the record had to be the embedded image. I just copied what was left of the record after the first 223 bytes to another byte[] and wrote it to disk, named it .jpg, and tada, I had the jpeg image!

    C# csharp json tutorial

  • How to retrieve non fixed-length records from a binary file
    H htres

    Thanks for the suggestions! Unfortunately, I am receiving this binary file from a third party and it's format is out of my control. :( The fields in the records seem to be seperated by a zero byte, and the records are seperated by a 16 byte string. Also, I noticed that the jpeg data starts with the bytes (in HEX) FF, D8, FF, E0 and ends with FF, D9. Could I possibly use these byte sequences to identify the jpeg? Also, I'm very new to this so all I have figured out how to do so far in code is to read my fixed length fields like so: FileStream fs = File.OpenRead(strFileName); BinaryReader reader = new BinaryReader(fs); //reads the first 36 bytes of trash reader.ReadBytes(36); //reads and stores the record delimiter string string strSignature = Encoding.ASCII.GetString(byteSignature); //advances the curser 1 byte reader.ReadBytes(1); //stores SKS ID string strSKSID = Encoding.ASCII.GetString(reader.ReadBytes(16)); //advances the curser 1 byte etc... reading down until I get to the image field.

    Luc Pattyn wrote:

    If you cant follow the above scheme (e.g. because the file format has been fixed and does not include a type byte), then you need to determine the type of the next record by reading and analyzing some bytes, then rewind a bit (using Seek method or Position property) and Load a record; repeat until done.

    I'm not sure how to actually implement your suggestion of reading and analyzing some bytes, then rewinding. Could you provide some example code? Thanks again!

    C# csharp json tutorial

  • How to retrieve non fixed-length records from a binary file
    H htres

    Hello, I'm new to programming and C#, so please bear with my ignorance! I need to extract jpeg images and header data from a binary file. The binary file is formatted with several fixed length fields containing information about the jpeg image, followed by the jpeg itself, followed by more header data, another jpeg, etc... Using a FileStream and BinaryReader I am able to read and store the metadata, because I know the length of the fields, but I am stumped on how to read and store the jpeg bytes since they vary in size. There is a fixed record delimiter between each header data/jpeg record, so I was thinking of using that to break apart the records. Once they are seperated and the header fields read, I could just assume the rest is the jpeg and store that. I'm not sure how to go about doing that though. Any suggestions or demo code is greatly appreciated! Thanks!

    C# csharp json tutorial
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups