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

    Reading a file as Hexadecimal

    I am still fairly new to C#. My background is in IBM mainframe. I have most of the basics down I think but here is what i am unsure of how to do.

    I have image files, could be one of 4 or 5 formats. I know from some research the header tag in hex will tell me if the file is a Tif or Jpeg or raster.

    IE (4D 4D 00 2A) is a Tif image.

    How do I read in this file and translate it to Hex?

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Reading a file as Hexadecimal

    Hex is just a view of the raw data, like binary, octal or decimal... or an image for that matter.

    I am guessing that what you really need to do is determine the format of the image file? ie, once you've read the first ulong (UInt64) of the file, you just want to look that up in a map code->ImageType? ie, the Hex bit is irrelevent. I understand that the headers are normally given in 4 byte Hex as they are easier to interpretate than decimal.

    Untested code follows...
    Code:
                // create a code to image type mapping table
                Dictionary<UInt64, String> imageFormatlookup = new Dictionary<UInt64, String>();
    
                // add all of the codes
                imageFormatlookup.Add(0x4D4D002A, "TIF");  // TODO: check the byte ordering is correct
                // etc etc
    
                // image file for demo purposes
                String imageFilename = @"C:\UnknownImageFile.dat";
    
                // read the raw data
                Byte[] rawData = File.ReadAllBytes(imageFilename);
    
                // map the data which is now in memory
                using (MemoryStream ms = new MemoryStream(rawData))
                {
                    // read from the memory in 'binary' format
                    using (BinaryReader br = new BinaryReader(ms))
                    {
                        // get the first 4 bytes
                        UInt64 imageFormat = br.ReadUInt64();
    
                        String imageType;
    
                        // see if the first 4 bytes represent an image file
                        if (imageFormatlookup.TryGetValue(imageFormat, out imageType))
                        {
                            //  process the rest of the file, it is of type imageType
                        }
    
                        br.Close();
                    }
    
                    ms.Close();
                }
    Last edited by rliq; December 22nd, 2009 at 06:27 PM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Aug 2008
    Posts
    112

    Re: Reading a file as Hexadecimal

    For a binary file format, I always read it in binary mode with available functions from File, BinaryReader etc classes, which provide obviously more correct reading and are easier to use than those available in MFC-C/C++.
    hi,,,

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