February 04, 2004

I forgot how fun maths can be

I forgot how fun maths can be!

Yes nerd alert for those reading this that aren't so inclined. That file format that I did a bit of reverse eng on has come back again. I had been getting some strange results when the file increased in size. I could see the value of the sixth byte of the output file increasing every 200 odd bytes I added to the input file. Odd in that it wasn't incremented every 256 bytes, in fact it wasn't incremented at any exact value, but it was always close to 256.

I then did a bit of brute force on it and wrote a little program that increasingly added bytes to the file and printed out the sixth byte of the output file. I could then see at what input file sizes the sixth byte of the output file incremented.

What I saw was quite interesting (annoying). When the input reached 214 bytes, byte 6 went from 0 to 1, and the next increments at 470, 726, 982 and 1238 bytes.

What I needed and ended up finding was an equation that I could plug in the number of bytes and it would give me the value to put into the sixth byte, byte 6 = f(number of bytes). I had hoped it would be a nice linear x = 256y,but I did enjoy the brain exercise and I think you will too. Try and work out the function. It isn't that hard .. and you will be pleasantly suprised (Simpsons quote).

.. and yes I did post this using Palm Weblogger

Update: Don't read the comments if you don't want to see the answers.

Posted by dbradby at February 4, 2004 08:39 AM
Comments

OK, Daniel. You sucked me in.

x = (y + M) mod 256

where y is the number of bytes and M is the answer to Life, The Universe and Everything (i.e. 42).

Posted by: Keith Pitty at February 4, 2004 10:14 AM

Oh so close but no cigar. Just a Longbeach menthol for the 42 aspect (that was the pleasant suprise).

Hint. I found it easier to express in code.

Posted by: DB at February 4, 2004 10:49 AM

Oops. High school maths was a very long time ago ;) How about:

int x = (int) (y + 42)/256;

Posted by: Keith Pitty at February 4, 2004 11:30 AM

Well it took me two goes but I got…

int bit6function (int size)
{
return (int) (size + 42)/256;
}
They say I’m little slow, EH?

Posted by: Mick Young at February 4, 2004 01:51 PM

Now do it in ASM.

And when you're done there, you have to write it to an EPROM using a cigarette lighter and a piece of purple celophane...*THEN* you can go out with my sister.


Anyone else having a slow day? :)

Posted by: Kris at February 4, 2004 02:48 PM