$b2m(N,[inp])[.out] - Convert b/kb/mb/gb

By Jonesy44 on Dec 11, 2008

This is an alias i created for going with my uTorrent add-on but i thought i'd release it on here as it's quite handy

Usage: $b2m(N,[inp])[.out]
Where the values "N", "inp" and ".out" represent;
N = The size in bytes.
inp = NOT MANDATORY; The input, if the value of N is NOT in bytes, enter the type here, "kb","mb","gb". Else the script will presume N is in bytes.
.out = NOT MANDATORY; But if you want it specifically in a certain output switch ".out" to ".b",".kb",".mb","gb". Else the script will output the most appropriate type.
Using input without ouput of course is pretty useless at will just resolve the same number as you entered.

For example;

$b2m(1024).kb
---
1 KB
$b2m(1,gb).mb
---
1024 MB

Image

alias b2m {
  ; Usage: $b2m(N,[inp])[.out]
  ; Where the values "N", "inp" and ".out" represent;
  ;     N    = The size in bytes.
  ;     inp  = NOT MANDATORY; The input, if the value of N is NOT in bytes, enter the type here, "kb","mb","gb". Else the script will presume N is in bytes.
  ;     .out = NOT MANDATORY; But if you want it specifically in a certain output switch ".out" to ".b",".kb",".mb","gb". Else the script will output the most appropriate type.
  ; Using input without ouput of course is pretty useless at will just resolve the same number as you entered.
  if ($1 isnum) {
    var %size = $1
    if ($istok(kb mb gb,$2,32)) {
      if ($2 == kb) { var %c = 1 }
      if ($2 == mb) { var %c = 2 }
      if ($2 == gb) { var %c = 3 }
      var %x = 1
      while (%x <= %c) { var %size = $calc(%size * 1024) | inc %x }
    }
    if ($istok(b kb mb gb,$prop,32)) { goto $prop }
    if (%size <= 1024) { :b | var %val = $round(%size,2) B }
    elseif ($calc(%size / 1024) <= 1024) { :kb | var %val = $round($calc(%size / 1024),2) KB }
    elseif ($calc(%size / 1024 / 1024) <= 1024) { :mb | var %val = $round($calc(%size / 1024 / 1024),2) MB }
    else { :gb | var %val = $round($calc(%size / 1024 / 1024 / 1024),2) GB }
    return %val
  }
  else { return error; $iif($1,$qt($1) is not a number,no number was entered) }
}

Comments

Sign in to comment.
Jonesy44   -  Dec 13, 2008

After following up on $bytes.. this is much better & more capable lol

 Respond  
Jonesy44   -  Dec 12, 2008

lmfao. i likes them square x]

I was kind of fearing there'd already be something for this :S too lazy to look in help might not have payed off this time :P

 Respond  
TheImrac   -  Dec 11, 2008

You sure do like to reinvent the wheel jonesy =)

 Respond  
napalm`   -  Dec 11, 2008

/help $bytes

 Respond  
Jonesy44   -  Dec 11, 2008

I used this alias in for this script; http://www.hawkee.com/profile/img/2454/
in the downloaded/size etc parts.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.