This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KrisRevi's profile picture

Calculating Bytes in a AE2 Storage Cell on CC

Started by KrisRevi, 03 July 2016 - 10:50 AM
KrisRevi #1
Posted 03 July 2016 - 12:50 PM
Calculating the bytes on the AE2 Me Drive / Storage Cell's are advanced or just simply STUPID when it comes to bytes and stuff what i've gathered so far is that

the first type of a new block takes up 513bytes

after that each block of that type takes 1 byte

so what i did was to get all the 63 types and multiply it with 513 and on the monitor i get 32319 bytes but on the cell itself it says 32760 Bytes (so im close but no cigar)!!! (this was with 64 stack of 63 items)
here im missing 441 Bytes

then i tryed with 1 block of each 63 and then i got 32319 Bytes on the monitor and on the cell i got 32264 Bytes (so im close AGAIN but no cigar)!!!
here im missing 55 Bytes

MY CODE:

local cell = "65536" -- Static, cause you cant get/fetch the size off the Storage Cells on CC or OpenPer or openCCsensor
local list = MED.getAvailableItems() -- This is a Table holding tables of each item in the Terminal/storage cell
local totalSize = 0 -- Just to 0 each loop

for number, item in pairs(list) do
totalSize = totalSize + item.size -- This counts the total amount of blocks
totalSize2 = number -- this prints out how many items are currently in the cell (63 atm)
end

draw_text(2, 5, "HDD: "..cell, colors.white, colors.black)
draw_text(2, 6, "Byte Used: "..totalSize, colors.white, colors.black)
draw_text(2, 7, "Type: "..totalSize2*513, colors.white, colors.black) -- here i multiply 63 with 513

there is also a flaw with the "types allowed" system on AE2 (OR A BUG NOT SURE YET, needs to test more)

a 64k Storage Cell holdes 63 different type at 65536 bytes of blocks

and i've tested out the type function, currently i get 63 on the AE2 when i look at the cell also i get 63 on CC monitor, but at our main Me terminal i currently hold 75 different items in 1 64k storage cell and apparently that is 63 types for the storage cell so i am PRETTY confused at the moment!!!

Maybe it has something to do with crafted stuff being more then 1 block or? i dunno!!!

could realy use some help here!!
Edited on 03 July 2016 - 10:55 AM
KrisRevi #2
Posted 03 July 2016 - 02:05 PM
Answer for this was to change this:

totalSize = totalSize + item.size

To this:
totalSize = totalSize + 512 + item.size / 8

+ 512 because for each new type of block that block would use 512 bytes and item.size / 8 cause foreach 8 blocks it takes up 1 byte
Edited on 03 July 2016 - 12:07 PM
KrisRevi #3
Posted 03 July 2016 - 04:42 PM
So i have this

local cell = 327680

this is the equal to 5 x 64k storage cells from (AE2) and i want a function that calculates how many B or KB or MB or GB or TR it is and shorten the number down and puts the "B"-"KB"-"MB"-"GB"-"TB" behind the result but i dunno how, as stated befor im a newbie/basic lua coder and dunno how this is done.
LBPHacker #4
Posted 03 July 2016 - 05:07 PM
local cell = 327680

local KILO = 1024
local prefixes = {"", "Ki", "Mi", "Gi", "Ti"}
local magnitude = math.floor(math.log(cell) / math.log(KILO))

local str = ("%.2f %sB"):format(cell / KILO ^ magnitude, prefixes[magnitude + 1])

print(str) -- or something

The only interesting thing in there is the double-math.log trick, the "Change of base formula", explained here.
KrisRevi #5
Posted 03 July 2016 - 07:36 PM
so 327680 ishows up as 320.0 KiB and that is correct but is there any way to only show it as 320 without the .0 ?
LBPHacker #6
Posted 03 July 2016 - 08:06 PM
Well, I'm just going to add a :gsub call here. I couldn't think of any way to do it with just :format.

local cell = 327680

local KILO = 1024
local units = {"B", "KiB", "MiB", "GiB", "TiB"}
local magnitude = math.floor(math.log(cell) / math.log(KILO))

local str = ("%.2f"):format(cell / KILO ^ magnitude):gsub("%.?0+$", "") .. units[magnitude + 1]

print(str) -- or something
KrisRevi #7
Posted 03 July 2016 - 08:45 PM
clear something up for me, google says that 167772160Bytes is 167Mb but the script you wrote gives me 160Mb on the monitor why?
LBPHacker #8
Posted 03 July 2016 - 09:00 PM
If that's the case, Google seems to use "business megabytes". Usually the K (kilo) prefix means "multiply this by 1000". Well, 1000 is not a power of 2, but 1024 is. So there are cases when a different prefix system is used, with the prefixes Ki (kibi), Mi (mebi), Gi (gibi), etc. This is the one I used in the code. 1 MiB = 1024 KiB = 1048576 B.

This is how you end up with 298 GB of space on a 320 GB external HDD. It's 320 business gigabytes, which translates to 298.02 gibibytes = something around 320000000000 bytes. Pretty awful, I know.
KrisRevi #9
Posted 03 July 2016 - 09:42 PM
Thanks for that info :)/> very usefull the :gsub worked on the static number but with my "bytes used" that comes directly from .getAvailableItems() comes out as 1.34098234092385096Mb why?

my code .getAvailableItems():

local list = MED.getAvailableItems()
local totalSize = 0

for number, item in pairs(list) do
totalSize = totalSize + 512 + item.size / 8
end

the exact numbers are

without your code 1159643.1
with your code 1.1061125993728638

the first block of a new item takes up 512Bytes on the cell and after that every 8 of that block types take up 8 Bytes, so how do i counter this with the script you wrote me?
Edited on 03 July 2016 - 07:49 PM
LBPHacker #10
Posted 03 July 2016 - 09:56 PM
Hmm. Funny. LuaJ doesn't seem to like %f.

[namedspoiler=It seems to be LuaJ's fault][img]http://i.imgur.com/BS7R88d.png[/img][/namedspoiler]

Well, in that case …
[code]local cell = 327680

local KILO = 1024
local units = {"B", "KiB", "MiB", "GiB", "TiB"}
local magnitude = math.floor(math.log(cell) / math.log(KILO))

local str = tostring(cell / KILO ^ magnitude):match("^(%d+%.?%d?%d?)%d*0*$") .. units[magnitude + 1]

print(str) – or something[/code]
Lignum #11
Posted 03 July 2016 - 10:19 PM
Hmm. Funny. LuaJ doesn't seem to like %f.

[namedspoiler=It seems to be LuaJ's fault][img]http://i.imgur.com/BS7R88d.png[/img][/namedspoiler]

Well, in that case …
[code]local cell = 327680

local KILO = 1024
local units = {"B", "KiB", "MiB", "GiB", "TiB"}
local magnitude = math.floor(math.log(cell) / math.log(KILO))

local str = tostring(cell / KILO ^ magnitude):match("^(%d+%.?%d?%d?)%d*0*$") .. units[magnitude + 1]

print(str) – or something[/code]
[/quote]

Indeed, [url="http://www.computercraft.info/forums2/index.php?/topic/6965-146sspsmp-stringformat-behaves-incorrectly-with-widthprecisions/"]this is a pretty old bug[/url].
KrisRevi #12
Posted 03 July 2016 - 11:01 PM
Thank you once again, that worked good :)/> im gonna post my entire script and ofcourse give you credit for your part :)/>
Edited on 03 July 2016 - 09:01 PM
Lyqyd #13
Posted 04 July 2016 - 04:23 AM
Threads merged. Please stick to one topic for all questions about a given program or piece of code.
KrisRevi #14
Posted 04 July 2016 - 01:13 PM
Threads merged. Please stick to one topic for all questions about a given program or piece of code.

yea sorry bout that :)/>