HaLo2FrEeEk Posted March 16, 2009 Share Posted March 16, 2009 Ok, I don't know if that's confusing or not, but I'll try to explain. Basically I need to open a file that contains a jpg file inside a container. The jpg file starts at offset E000 in the file, everything before that is header and information data for the rest of the file, but the JPG header starts at E000. Here's what 've got as my code so far: using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void button1_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { //setup reader FileStream fs = new FileStream(ofd.FileName, FileMode.Open); BinaryReader br = new BinaryReader(fs); long length = fs.Length; //example read br.BaseStream.Position = 0x0000E000; byte[] example = br.ReadBytes(800); TextBox1.Text = BitConverter.ToString(example); } } } } Now, when I print out the byte array to the textbox using this method (at the bottom) I get a string of hex values with dashes between the bytes. It reads all 800 values though. When I use this method: TextBox1.Text = Encoding.Default.GetString(example); It prints out the characters up to the first 00 hex byte, not including that byte. In the case of a JPEG header the first 00 byte is the 5th byte, meaning this only prints out the first 4. I need to know how to make that not happen. I plan on saving the file, so it needs to read the whole thing. ALSO, I need to read from E000 to the end of the file and I can't for the life of me figure out how to go to the end of the file. I've tried while and do until loops, but VB would let me just say do until results.length, C# won't let me do that since length is a LONG. Quote Link to comment https://forums.phpfreaks.com/topic/149601-binaryreader-in-c-getting-data-from-offset-x-to-end-of-file/ Share on other sites More sharing options...
corbin Posted March 16, 2009 Share Posted March 16, 2009 The ReadBytes method reads the input number of bytes from the current position, yes? If so, number of bytes until the end = total bytes - current byte location. You know the current location since you set the location with the Position setting. Quote Link to comment https://forums.phpfreaks.com/topic/149601-binaryreader-in-c-getting-data-from-offset-x-to-end-of-file/#findComment-785611 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 16, 2009 Author Share Posted March 16, 2009 Now I need to figure out how to get the length of the file. I've tried .length and len and everything else I can find, .length returns a Long which can't be implicitly converted to a string. I didn't think you could perform math operations on a Long. Quote Link to comment https://forums.phpfreaks.com/topic/149601-binaryreader-in-c-getting-data-from-offset-x-to-end-of-file/#findComment-785810 Share on other sites More sharing options...
corbin Posted March 16, 2009 Share Posted March 16, 2009 I'm sure it's not difficult to convert a long to a string. In C++ it's all of like 3 lines. Of course you can do math on a long. It's a numeric type. (Isn't a long a 64 bit signed int in C#?) By the way, why would you need to convert the length to a string? http://msdn.microsoft.com/en-us/library/system.io.filestream_properties.aspx It appears that the FileStream should have a .Length property, but obviously you know that since you're using it in your code. So what exactly is your problem with filelength? Quote Link to comment https://forums.phpfreaks.com/topic/149601-binaryreader-in-c-getting-data-from-offset-x-to-end-of-file/#findComment-786226 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 17, 2009 Author Share Posted March 17, 2009 I was trying to print the value of .length to my textbox so I could confirm it was what I needed, that's why I needed it as a string. I'll try doing something like this: length - 0x0000E000 And see what that gives me. Quote Link to comment https://forums.phpfreaks.com/topic/149601-binaryreader-in-c-getting-data-from-offset-x-to-end-of-file/#findComment-786340 Share on other sites More sharing options...
corbin Posted March 17, 2009 Share Posted March 17, 2009 Does the string class in C# not have a long constructor? And that will give you how many bytes are left to read after E000. Quote Link to comment https://forums.phpfreaks.com/topic/149601-binaryreader-in-c-getting-data-from-offset-x-to-end-of-file/#findComment-786346 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 17, 2009 Author Share Posted March 17, 2009 You know what, I found an amazing writeup on the structure of the files I'm trying to read. Turns out everything I need is already there, like the offsets, length of content, etc. My only problem now is trying to get at that information as some of it is meant to be read as decimal and some as hex. Like, I can find out where the content starts by using a little math. If the 2 byte values starting at offset 0xC032 are equal to FFFF, then the list of contents starts at 0xC000, otherwise it starts at 0xD000, then I just use some math and from that I can figure out where the actual content starts and the length of it. Usually the length of the content is the length of the file - the content start, meaning you're right that I need to find filelength and subtract the byte value for offset D000. Now, what to do... Quote Link to comment https://forums.phpfreaks.com/topic/149601-binaryreader-in-c-getting-data-from-offset-x-to-end-of-file/#findComment-786734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.