Jump to content




Calculating Bytes in a AE2 Storage Cell on CC



13 replies to this topic

#1 KrisRevi

  • Members
  • 14 posts

Posted 03 July 2016 - 11:50 AM

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 by KrisRevi, 03 July 2016 - 11:55 AM.


#2 KrisRevi

  • Members
  • 14 posts

Posted 03 July 2016 - 01: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 by KrisRevi, 03 July 2016 - 01:07 PM.


#3 KrisRevi

  • Members
  • 14 posts

Posted 03 July 2016 - 03: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.

#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 July 2016 - 04: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.

#5 KrisRevi

  • Members
  • 14 posts

Posted 03 July 2016 - 06: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 ?

#6 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 July 2016 - 07: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


#7 KrisRevi

  • Members
  • 14 posts

Posted 03 July 2016 - 07: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?

#8 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 July 2016 - 08: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.

#9 KrisRevi

  • Members
  • 14 posts

Posted 03 July 2016 - 08: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 by KrisRevi, 03 July 2016 - 08:49 PM.


#10 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 03 July 2016 - 08:56 PM

Hmm. Funny. LuaJ doesn't seem to like %f.

It seems to be LuaJ's fault

Well, in that case ...
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


#11 Lignum

  • Members
  • 558 posts

Posted 03 July 2016 - 09:19 PM

View PostLBPHacker, on 03 July 2016 - 08:56 PM, said:

Hmm. Funny. LuaJ doesn't seem to like %f.

It seems to be LuaJ's fault

Well, in that case ...
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

Indeed, this is a pretty old bug.

#12 KrisRevi

  • Members
  • 14 posts

Posted 03 July 2016 - 10:01 PM

Thank you once again, that worked good :) im gonna post my entire script and ofcourse give you credit for your part :)

Edited by KrisRevi, 03 July 2016 - 10:01 PM.


#13 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 04 July 2016 - 03:23 AM

Threads merged. Please stick to one topic for all questions about a given program or piece of code.

#14 KrisRevi

  • Members
  • 14 posts

Posted 04 July 2016 - 12:13 PM

View PostLyqyd, on 04 July 2016 - 03:23 AM, said:

Threads merged. Please stick to one topic for all questions about a given program or piece of code.

yea sorry bout that :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users