1. Converting bytes
- Posted by doncCcole Jul 06, 2016
- 2193 views
I'm reading my drives and folders.
UDED=CAPACITY-FREE
All the answers in bytes.
What's the formula to convert them to Gigs, Megs, or Ks, whichever is appropriate? if amt>gig then if amt>K then if amt>MEG then end if
I am confused by the following:
Name | A | Bin Pwr | Binary Value | Decimal Power | Decimal Value | |
---|---|---|---|---|---|---|
Kilobyte | KB | 2^10 | 1,024 | 10^3 | 1000 | |
Megabyte | Mb | 2^20 | 1,048,576 | 10^6 | 1,000.000 | |
Gigabyte | GB | 2^30 | 1,073,741,825 | 10^9 | 1,000,000,000 | |
Terabyte | TB | 2^40 | 1,099,511,627,776 | 10^12 | 1,000,000,000,000 |
GIG,MEG,K to represent Binary Values
and gig,meg,k to represent Decimal Values
if amt>GIG then --GIG handling routine elsif amt>MEG then --MEG handling routine elsif amt>K then --K handling routine end if
also I could
if amt>gig then --gig handling routine elsif amt>meg then --meg handling routine elsif amt>k then --k handling routine end if
This is as far as I can get.
I don't know where to start .
Don Cole
2. Re: Converting bytes
- Posted by jimcbrown (admin) Jul 06, 2016
- 2192 views
What's the formula to convert them to Gigs, Megs, or Ks, whichever is appropriate?
if amt>GIG then --GIG handling routine elsif amt>MEG then --MEG handling routine elsif amt>K then --K handling routine end if
This is as far as I can get.
I don't know where to start .
Like so:
constant GIG=power(2, 30), MEG=power(2, 20), KIL=power(2, 10) atom temp if amt>GIG then --GIG handling routine temp = amt / GIG printf(1, "%g GIG\n", {temp}) elsif amt>MEG then --MEG handling routine temp = amt / MEG printf(1, "%g MEG\n", {temp}) elsif amt>KIL then --K handling routine temp = amt / KIL printf(1, "%g KIL\n", {temp}) else temp = amt printf(1, "%g BYT\n", {temp}) end if
I am confused by the following:
GIG,MEG,K to represent Binary Values
and gig,meg,k to represent Decimal Values
If you are not sure then you probably want the binary version (1024 etc) and not the decimal version.
3. Re: Converting bytes
- Posted by ghaberek (admin) Jul 06, 2016
- 2230 views
Here's the method I've been using for like, ever...
Units are expressed as either powers of 1000 (SI unit notation) or powers of 1024 Binary prefix notation) which makes this a very simple math problem.
- Create a list of suffixes in their ascending order.
- Divide the size value down by 1000 (or 1024) until it's less than 1000 (or 1024).
- Each time the size is divided, increment the index counter (moving the suffix up one level).
- Adjust the suffix to include "B" (for 1000) or "iB" (for 1024).
- Format the number and tack the suffix on the end.
include std/console.e include std/utils.e -- format types constant FORMAT_SI = 1000 constant FORMAT_BN = 1024 -- format suffixes constant SUFFIX = {"bytes","K","M","G","T","P","E","Z","Y"} function format_size( atom size, integer ftype = FORMAT_BN ) integer index = 1 while size >= ftype do -- ftype is 1000 or 1024 size /= ftype index += 1 end while sequence suffix = SUFFIX[index] if index > 1 then suffix &= iff( ftype = FORMAT_BN, "iB", "B" ) end if return sprintf( "%.3g %s", {size,suffix} ) end function procedure test( atom size ) sequence str1 = format_size( size, FORMAT_SI ) sequence str2 = format_size( size, FORMAT_BN ) printf( 1, "%10d %10s %10s\n", {size,str1,str2} ) end procedure procedure main() ------------------ -- | SI units | Binary | test( 0 ) -- | 0 bytes | 0 bytes | test( 10000 ) -- | 10 KB | 9.77 KiB | test( 65536 ) -- | 65.5 KB | 64 KiB | test( 10000000 ) -- | 10 MB | 9.54 MiB | test( 16777216 ) -- | 16.8 MB | 16 MiB | test( 1000000000 ) -- | 1 GB | 954 MiB | test( 1073741824 ) -- | 1.07 GB | 1 GiB | maybe_any_key() end procedure main()
-Greg
4. Re: Converting bytes
- Posted by doncCcole Jul 08, 2016
- 2112 views
Here's the method I've been using for like, ever...
include std/console.e include std/utils.e -- format types constant FORMAT_SI = 1000 constant FORMAT_BN = 1024 -- format suffixes constant SUFFIX = {"bytes","K","M","G","T","P","E","Z","Y"} function format_size( atom size, integer ftype = FORMAT_BN ) integer index = 1 while size >= ftype do -- ftype is 1000 or 1024 size /= ftype index += 1 end while sequence suffix = SUFFIX[index] if index > 1 then suffix &= iff( ftype = FORMAT_BN, "iB", "B" ) end if return sprintf( "%.3g %s", {size,suffix} ) end function procedure test( atom size ) sequence str1 = format_size( size, FORMAT_SI ) sequence str2 = format_size( size, FORMAT_BN ) printf( 1, "%10d %10s %10s\n", {size,str1,str2} ) end procedure procedure main() ------------------ -- | SI units | Binary | test( 0 ) -- | 0 bytes | 0 bytes | test( 10000 ) -- | 10 KB | 9.77 KiB | test( 65536 ) -- | 65.5 KB | 64 KiB | test( 10000000 ) -- | 10 MB | 9.54 MiB | test( 16777216 ) -- | 16.8 MB | 16 MiB | test( 1000000000 ) -- | 1 GB | 954 MiB | test( 1073741824 ) -- | 1.07 GB | 1 GiB | maybe_any_key() end procedure main()
-Greg
Thank you very much Greg,
I would love to try you code.
But right now I only have Euphoria 3.1 working.
I'm having problems with 4.1.
Could you repost your code in the 3.1 form?
Don Cole
5. Re: Converting bytes
- Posted by jmduro Jul 08, 2016
- 2089 views
Could you repost your code in the 3.1 form?
It is not that complicated to convert Eu 4 code. Here it is:
include get.e -- format types constant FORMAT_SI = 1000 constant FORMAT_BN = 1024 -- format suffixes constant SUFFIX = {"bytes","K","M","G","T","P","E","Z","Y"} function format_size( atom size, integer ftype ) integer index sequence suffix index = 1 while size >= ftype do -- ftype is 1000 or 1024 size /= ftype index += 1 end while suffix = SUFFIX[index] if index > 1 then if ( ftype = FORMAT_BN ) then suffix &= "iB" suffix &= "B" end if end if return sprintf( "%.3g %s", {size,suffix} ) end function procedure test( atom size ) sequence str1, str2 str1 = format_size( size, FORMAT_SI ) str2 = format_size( size, FORMAT_BN ) printf( 1, "%10d %10s %10s\n", {size,str1,str2} ) end procedure procedure main() integer ok ------------------ -- | SI units | Binary | test( 0 ) -- | 0 bytes | 0 bytes | test( 10000 ) -- | 10 KB | 9.77 KiB | test( 65536 ) -- | 65.5 KB | 64 KiB | test( 10000000 ) -- | 10 MB | 9.54 MiB | test( 16777216 ) -- | 16.8 MB | 16 MiB | test( 1000000000 ) -- | 1 GB | 954 MiB | test( 1073741824 ) -- | 1.07 GB | 1 GiB | ok = wait_key() end procedure main()
Jean-Marc
6. Re: Converting bytes
- Posted by ghaberek (admin) Jul 08, 2016
- 2054 views
Could you repost your code in the 3.1 form?
It is not that complicated to convert Eu 4 code. Here it is:
I think you're missing an else here:
suffix = SUFFIX[index] if index > 1 then if ( ftype = FORMAT_BN ) then suffix &= "iB" else -- <-- was missing suffix &= "B" end if end if
-Greg
7. Re: Converting bytes
- Posted by doncCcole Jul 08, 2016
- 2058 views
Could you repost your code in the 3.1 form?
It is not that complicated to convert Eu 4 code. Here it is:
I think you're missing an else here:
suffix = SUFFIX[index] if index > 1 then if ( ftype = FORMAT_BN ) then suffix &= "iB" else -- <-- was missing suffix &= "B" end if end if
-Greg
Thank for that suggestion.
The problem I'm having is:
E:\EU\myInclude\console.e:6
namespace has not been declared
namespace console
Thanks again, Don Cole
8. Re: Converting bytes
- Posted by jmduro Jul 12, 2016
- 1978 views
The problem I'm having is:
E:\EU\myInclude\console.e:6
namespace has not been declared
namespace console
Remove that line from console.e.
In the calling program replace "include console.e" by "include console.e as console".
Jean-Marc
9. Re: Converting bytes
- Posted by petelomax Jul 13, 2016
- 1950 views
Remove that line from console.e.
In the calling program replace "include console.e" by "include console.e as console".
That will almost certainly not help.
The error suggests (cmiiw) an interpreter <=Eu3 has been given an include from >=Eu4, and further it seems that Don has tried to create E:\EU\myInclude\ as an amalgamation of include\std\ and include\, that will somehow be Eu3 and Eu4 compatible - and without meaning to be rude, I am unfortunately in no position to help anyone extricate themselves from that sort of foolishness.
Pete
10. Re: Converting bytes
- Posted by jmduro Jul 13, 2016
- 1945 views
The error suggests (cmiiw) an interpreter <=Eu3 has been given an include from >=Eu4, and further it seems that Don has tried to create E:\EU\myInclude\ as an amalgamation of include\std\ and include\, that will somehow be Eu3 and Eu4 compatible - and without meaning to be rude, I am unfortunately in no position to help anyone extricate themselves from that sort of foolishness.
A port o std/console to Eu3? Not easy at all. I use klrconie.e with some additional routines for mine.
Jean-Marc
11. Re: Converting bytes
- Posted by doncCcole Jul 13, 2016
- 1912 views
The error suggests (cmiiw) an interpreter <=Eu3 has been given an include from >=Eu4, and further it seems that Don has tried to create E:\EU\myInclude\ as an amalgamation of include\std\ and include\, that will somehow be Eu3 and Eu4 compatible - and without meaning to be rude, I am unfortunately in no position to help anyone extricate themselves from that sort of foolishness.
A port o std/console to Eu3? Not easy at all. I use klrconie.e with some additional routines for mine.
Jean-Marc
Remove that line from console.e.
In the calling program replace "include console.e" by "include console.e as console".
Jean-Marc
Thank you Jean-Marc and Pete,
This all has become a mute issue.
Because now, I finally have both Euphoria 3.1 and Euphoria 4.1 working on one computer. Not a simple task(for me).
Anyway Jean-Marc, I ran your code and it seems to be exactly what I need. To do what I'm try to do. That is print out drive and folder sizes in Byte, K, Meg or Gig form.
Thanks again,
Don Cole