HaLo2FrEeEk Posted March 17, 2009 Share Posted March 17, 2009 Ok, the file I'm reading has a header and footer, and in between is the data I need, which is a png image. In the header there is the length of the png file within 4 hex bytes starting at offset 0x40. The png starts at offset 0x44. I need to be able to read in those 4 bytes, get their decimal value so I can know how many bytes there are, then I need to pass that value to readbytes so that it can read that many bytes starting from offset 0x44. Here is an example: @ 0x40, 4 Hex Byte Array: 00 00 A3 E1 That converts to 41953 in decimal That means that starting at offset 0x44, I need to read 41,953 bytes to get to the end of the data, after that is the footer. But for the love of god I can't do it. It won't let me convert from a byte array to decimal, and I've tried converting to string, int, etc, it won't let me!?! I know it's possible, there are programs that do what I'm trying to do (I'm trying to do it so I can learn the language), but HOW? Please please PLEASE someone help me! Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/ Share on other sites More sharing options...
corbin Posted March 17, 2009 Share Posted March 17, 2009 To be technical, you should probably use that as an unsigned int. But anyway.... A char is simply a byte when talking about single byte chars, so, you can convert a char to another numeric type (yeah, a char is basically just a numeric type.... it's essentially an 8 bit integer). So, you can cast a byte to an int. That doesn't help you since you could do it four times but that leaves you with 4 ints. That's where bitwise magic comes into play. So, let's say you have 4 bytes.... b1, b2, b3, b4. Let's say you concatenate them as a string such as b1b2b3b4 then convert them from hex to decimal.... That's easy enough, but it's also terribly inefficient. What you should realize: Surely by now you've been exposed to binary, but in case not, I shall explain. In base 10, you know for each place you multiply by 10, yes? 41953 for example is 4 * 10^4 + 1 * 10^3 + 9*10^2 + 5*10^1 + 3*10^0 Well actually I'm not going to explain binary since I don't need to. Now, in hex, you know that. 1AB90 (109456 base 10) in decimal is 1*16^4 + 10*16^3 + 11*16^2 + 9*16^1 + 0*16^0 1*16^4 = 65536 10*16^3 = 40960 11*16^2 = 2816 9*16^1 = 144 0*16^0 = 0 Add all those together: 109456 So hopefully you'll understand the logic behind this: For a collections of bytes b1, b2, b3, b4, the value of them if they were to be considered 1 chunk of something could be determined by: b4 + (b3 << + (b2 << 16) + (b1 << 24) Or, a more concrete example: 00 00 A3 E1 b1 b2 b3 b4 A3 = 163 E1 = 225 225 + (163 << x << y is equivalent to multiplying x by 2 y times since it adds a bit, therefor, x << y = x * 2^y. So, 225 + 163*2^8 = 41953. I don't know how it would be coded in C#, but I would imagine it's similar to how it would be done in C. In C, it would look something like: byte[] b = {0x00, 0x00, 0xA3, 0xE1}; unsigned int i = b[3] + (b[2] << + (b[1] << 16) + (b[0] << 24); Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-787153 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 18, 2009 Author Share Posted March 18, 2009 I got it, I asked for help on another forum that commonly works with these kinds of files. Turns out the values need to be read in Big Endian, so this: byte[] pnglen = br.ReadBytes(4); Array.Reverse(pnglen, 0); int offset = Convert.ToInt32(pnglen); Gave me back the numerical value that was exaactly what I needed. Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-787424 Share on other sites More sharing options...
corbin Posted March 18, 2009 Share Posted March 18, 2009 I hate Endianness. It's evil ;p. Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-788068 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 18, 2009 Author Share Posted March 18, 2009 True dat. Another question, if I might. I can't figure out how to write the pngbytes byte array to a file. It contains the bytes from the offset 0x44 through the length aquired from the 4 bytes starting at 0x40. I don't know how to write it to a file though. I've got the savefiledialog and all that, I just need to know how to do wpng.Write(?). Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-788119 Share on other sites More sharing options...
corbin Posted March 18, 2009 Share Posted March 18, 2009 Hrmmm, not sure exactly since I have 0 C# experience, but from some brief googling, I've managed to come up with this: //pretend b is a byte array containing what you read from the original file. FileStream f = new FileStream("out.png", FileMode, FileAccess); BinaryWriter br = new BinaryWriter(f); br.Write(b); br.close(); Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-788127 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 19, 2009 Author Share Posted March 19, 2009 I love you. Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-788635 Share on other sites More sharing options...
corbin Posted March 19, 2009 Share Posted March 19, 2009 Amazing what 30 seconds of googling can turn up, eh? ;p Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-788858 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 20, 2009 Author Share Posted March 20, 2009 I so googled that, I'd show you my history but I delete it after a day. I couldn't find it anywhere! Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-789069 Share on other sites More sharing options...
corbin Posted March 20, 2009 Share Posted March 20, 2009 Eh I just knew what to look for hehe. Found a link on msdn, saw the FileStream class then looked for classes that took it as an input. Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-789072 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 20, 2009 Author Share Posted March 20, 2009 Thank you all the same. Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-789589 Share on other sites More sharing options...
corbin Posted March 21, 2009 Share Posted March 21, 2009 No problem ;p. Messing with languages I don't know entertains me. No telling how many people's future in some random language I've impeded ;p. Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-790105 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 21, 2009 Author Share Posted March 21, 2009 If you're interested, here is the program: http://infectionist.com/misc/openBLF.exe And a test file that you can open in it: http://infectionist.com/misc/c_005.blf If you open that in a hex editor you'll notice that a JPEG header starts at offset 0x44, well starting at offset 0x40 the 4 bytes there are the size of the file in bytes, in big endian. This app just gets that value and then reads the bytes from 0x44 to the length specified from 0x40. The resulting image file will be saved to the same directory the BLF file is in and with the name [original filename].png. The batch feature doesn't work yet and the program will only open BLF files. Thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/149882-solved-c-hex-byte-array-to-decimal-to-int/#findComment-790476 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.