• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Large numbers in binary files?

rytan451

Member
A quick primer: http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04s10.html

Binary files represent numbers using binary (unsurprisingly). Binary numbers are base-2, much like our traditional counting system uses base-10, or time uses base-60 (60 seconds in a minute, 60 minutes in an hour).

In base-10, we count 0, 1, 2, 3, 4 ... 9. Then, we'd want to count the next number, but we can't do that in a single digit, since this is base 10, and base 10 doesn't have a single digit for the number 10. So, we bring the rightmost number back to 0, and increase the number to the left by 1. So, 9 (which has an implicit 0 behind it, like 09) would go to 10.

In base-2, we only have 0, 1. We'd want to count the next number, but again, we can't do that in a single digit, because in base 2, we don't have a single digit for the number 2. In base 2, we only have digits for 0 and 1. So counting from 0 to 10 (decimal numbers) in binary would look like this: 0, 1, then we add another digit: 10, 11, again add another digit 100, 101, 110, 111, again add another digit 1000, 1001, 1010.

In binary, 255 looks like 11111111. A byte is basically a block of 8 bits (binary digits, basically in this context). If you want to represent 256, you need to add another digit: 100000000. But, a byte only has 8 bits, and you need 9, so you want to add another byte. (It's possible to avoid adding another whole byte, but it's hard, and you might add the byte anyways)

So, you'd have 00000001 00000000 (For convenience, I separated the bytes) for 256.

8564 would be 00100001 01110100 in binary, needing two bytes.

By the way, file-writing or file-reading operations are each very slow. If you're writing to a binary file, you'd probably want to write your data to a buffer, which lives in RAM, then write the entire buffer to the file on the hard drive. That's much faster than writing the data byte-by-byte.

Code solution:

Code:
var buff = buffer_create(2, buffer_grow, 1)

// Write some other things that you want in the file
buffer_write(buff, buffer_u16, 8564)
// Write some other things you want in the file after 8546

buffer_save(buff, working_directory + "your_file.bin")
buffer_destroy(buff)
 
Last edited:
Top