Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Writing binary numbers to a file?

Author
Message
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 30th Apr 2008 09:09 Edited at: 31st May 2008 15:50
Hello

Is it possible to write binary numbers to a file? I want to do this so I can save space when saving a file in my model maker.

Example:

The number 255 will be saved as a binary like this:01000000 01100000 0110000
But the numbers from 0 to 9 don`t need to take up 8 bits: 0100 0110 0110
By using this, I can half the diskspace required.

So now, I need to be able to write those four bits to a file. Is this possible in DBC?


Thanks, TheComet

Oooooops!!! I accidentally formated drive c.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 30th Apr 2008 11:07
The decimal number 255 will be stored like this: 11111111 (8 bits = 1 byte).
If you want to save each decimal digit separately you'd need 4 bits for each digit (0-9 / 0000 to 1001). That would mean a 3 digit number requires 150% of the original space.

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th Apr 2008 11:23 Edited at: 30th Apr 2008 11:30
[EDIT was writing this while empty was responding - I'm assuming a custom bit configuration 8 8 7 to be reduced when necessary to 4 4 4]

The smallest number you can write directly to a file is 1 byte or 8 bits in DBC. However, if you have a series of 4 bit numbers, you can store them in two groups of 4 or 1 byte in a memblock. Then write the entire memblock to a file at once. You'd have to do the conversion in and out to get the number you wanted.

This example uses TDKs binary to decimal string converter function. This example isn't optimized and just shows how to get the numbers in and out of the memblock once you've reduced them to their 4 bit values. Because your example shows 3 4 bit groups, I had to add a zero fill to make the last value a whole byte. Depending on the number of inputs, you would only need the 0000 fill at the end of the memblock if the number of inputs was uneven.



Enjoy your day.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st May 2008 15:23
@ Empty

Quote: "That would mean a 3 digit number requires 150% of the original space."


Sorry, but I have to disagree with that. It would take up half of the space.

@Latch
I would love to use that, and I understand it, but as you may know, I can`t upgrade and therefore not use memblocks. Thanks, anyway!

TheComet

Oooooops!!! I accidentally formated drive c.
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 1st May 2008 17:12 Edited at: 1st May 2008 17:17
Quote: "Sorry, but I have to disagree with that. It would take up half of the space."

Your method would only require half the space if you want to store each digit separatly. While I don't know the reason why you would want to do this, you can store 2 digits in a byte (which is the smallest data size you can write as Latch mentioned) without memblocks. For a 3 digit number you would still need 2 bytes but still. After you've bundled 2 digits with Digits2Byte you can write that byte to a file.



There are certainly more efficient methods (regarding both speed and required space) but for that I'd need to know the way you store your data.

Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st May 2008 22:31
Clunk! (hits palm to forehead) Like empty said, you can write directly to the file with WRITE BYTE or depending on your grouping of bytes WRITE WORD (2 bytes) or WRITE LONG (4 bytes) - you don't need memblocks. I was thinking of the memblock like an array of bytes for storage, addition, and modification before you actually write the file.

Note: I was using binary strings so you could see the bit values. String conversions back and forth are not very efficient. empty's example is more to the point.

Quote: "but as you may know, I can`t upgrade and therefore not use memblocks. Thanks, anyway!"


Sorry, I can't keep it straight as to who has which version of DBC. If you can prove your purchase of DB then you should be able to get 1.20 from support. This isn't an upgrade, it's actually a complete version that includes the enhancements so it shouldn't be subject to the problems with the 30 day enhancement trial glitch that made legitimate copies of DBC enhanced fail after 30 days. If you already have a purchase history, it should be no problem - but I believe you have to get the version from support, it's not available in the upgrades section of TGC.

Enjoy your day.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd May 2008 14:55
Quote: "If you already have a purchase history..."


Yeah, that is my problem. I bought DarkBasic from a store on a CD for 20$.

Oooooops!!! I accidentally formated drive c.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd May 2008 19:21
@ WindowsKiller

Hey, cool! Thanks a lot! I appreciate your effort for helping me out! Theres only one question: Is this the broken installer?

Many thanks, TheComet

Oooooops!!! I accidentally formated drive c.

Login to post a reply

Server time is: 2025-06-07 13:13:59
Your offset time is: 2025-06-07 13:13:59