New bytecalc-function (for PB 4.01+)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

New bytecalc-function (for PB 4.01+)

Post by AND51 »

Hello!

Here's my new bytecalc() function for PB 4.01+. (Don't try to beat this, the german-forum-competition is over :D and I've won).

This procedure turns a quad number of bytes into the greatest unit.
Eample:
1024 Byte => 1 KB
16777216 => 16 MB.

You can optionally switch on decimals numbers; the procedure automatically enforces no decimal numbers, if the greatest unit is "Byte", because it doesn't make sense to have 3.67 Byte, does it? :wink:

Code: Select all

Procedure.s bytecalc(byte.q, NbDecimals.b=0)
	Protected unit.b=Round(Log(byte)/Log(1024), 0)
	ProcedureReturn StrD(byte/Pow(1024, unit), NbDecimals*(unit And 1))+" "+StringField("Byte,KB,MB,GB,TB,PB,EB", unit+1, ",")
EndProcedure

Debug bytecalc(FileSize("C:\pagefile.sys"))
Debug bytecalc(3.67*1024)
Debug bytecalc($FFFFFF)
Debug bytecalc(1024)
Do you think, this code can be added to PB as a "native function"? :)
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Basically taking bytes as a quad is no use because you're converting to a double inside the function.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

hi,

and what about this one ?

Code: Select all

Import "shlwapi.lib"
  CompilerIf #PB_Compiler_Unicode
  bytecalc_(number.q, *buffer, length.l) As "_StrFormatByteSizeW@16"
  CompilerElse
  bytecalc_(number.q, *buffer, length.l) As "_StrFormatByteSize64A@16"
  CompilerEndIf
EndImport

Macro bytecalc(num, lbuf = 255)
  PeekS(bytecalc_(num, @buf.s{lbuf}, lbuf), lbuf)
EndMacro


For i = 0 To 62
  Debug bytecalc(Pow(2, i))
Next
yes but i must admit it is not cross-platform !

but just one question :
what the hell means the 'P'b and 'E'b ?

Kb -> Kilo Bytes
Mb -> Mega Bytes
Gb -> Giga Bytes
Tb -> Tera Bytes
Pb -> ?
Eb -> ?
Last edited by Flype on Fri Nov 03, 2006 10:12 pm, edited 1 time in total.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

ah yes - i found it !

it is :

Pb -> Peta Bytes

Eb -> Exa Bytes

you can verify it on wikipedia :
http://en.wikipedia.org/wiki/SI_prefix
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Trond wrote:Basically taking bytes as a quad is no use because you're converting to a double inside the function.
Yes, because the user might want to have decimal numbers; this couldn't be realized with not using doubles.


@ flype: I've no knowlege about the import-function, so I cannot comment your solution. However, it seems to work, I tested it.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

http://en.wikipedia.org/wiki/Binary_prefix

I would recommend following the IEC standard prefixes instead to avoid confusion with the SI prefixes.
thamarok
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 06, 2006 1:37 pm

Post by thamarok »

Flype wrote:ah yes - i found it !

it is :

Pb -> Peta Bytes

Eb -> Exa Bytes

you can verify it on wikipedia :
http://en.wikipedia.org/wiki/SI_prefix
No, Pb is PureBasic :P
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

AND51 wrote:
Trond wrote:Basically taking bytes as a quad is no use because you're converting to a double inside the function.
Yes, because the user might want to have decimal numbers; this couldn't be realized with not using doubles.
But then you can't take a byte count that is as large as a quad.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Trond wrote:
AND51 wrote:
Trond wrote:Basically taking bytes as a quad is no use because you're converting to a double inside the function.
Yes, because the user might want to have decimal numbers; this couldn't be realized with not using doubles.
But then you can't take a byte count that is as large as a quad.
Correct, but this procedure hs been designed for programs for home PCs. ´has your PC files that are larger than 1 TB? I don't think so. So you can use my procedure without worries.

Even if you want the full power, then use flype's procedure.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Rescator wrote:http://en.wikipedia.org/wiki/Binary_prefix

I would recommend following the IEC standard prefixes instead to avoid confusion with the SI prefixes.
Read it - got confused.

Does it mean that 1Kb = 1000 and 1KiB = 1024 (the real K!)? And what is a Ko (apart from a knock out)?
Dare2 cut down to size
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

I won't use KiB, MiB, GiB, etc. it's just confusing.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Dare wrote:
Rescator wrote:http://en.wikipedia.org/wiki/Binary_prefix

I would recommend following the IEC standard prefixes instead to avoid confusion with the SI prefixes.
Read it - got confused.

Does it mean that 1Kb = 1000 and 1KiB = 1024 (the real K!)? And what is a Ko (apart from a knock out)?
Although computer data is normally measured in binary code, the prefixes for the multiples are based on the metric system. The nearest binary number to 1,000 is 2^10 or 1,024; thus 1,024 bytes was named a Kilobyte. So, although a metric "kilo" equals 1,000 (e.g. one kilogram = 1,000 grams), a binary "Kilo" equals 1,024 (e.g. one Kilobyte = 1,024 bytes). Not surprisingly, this has led to a great deal of confusion.
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
stubbsi
User
User
Posts: 50
Joined: Tue Jul 04, 2006 8:59 pm
Location: Mt Martha, Australia

Post by stubbsi »

pariticularly when the EU got involved and decided that a kb was 1000 bytes not 1024.

logical - eh!! bureaucracy gone mad - obviously don't know that 2^10 = 1024.
Vincit qui primum gerit
"The Old Farts Wins" or "He Conquers Who First Grows Old"
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

As long as you remain consistent within your program (i.e. do NOT mix KB and KiB etc.) then most users wont mind whether you use KB or KiB. It might be a nice touch to mention in the user documentation for your programs whether KB indicate KB or KiB though.
By users I here mean end users, as a programmer (PB user) I sure hope we know/see the difference what is KB or KiB :)
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Hi GeoTrail, stubbsi and Rescator,

Thanks. So officially a KiB = 1024?

And a Ko is .. what?

lol. Bring back feet and inches and tons and gallons and furlongs! And shillings and pence for that matter! (This program is 27 acres zipped and requires 132 acres of diskspace when decompressed. It costs 8 pounds seven shillings and sixpence - download now?)
Dare2 cut down to size
Post Reply