Skip to content
⌘ NSIS Forum Archive

passing bytes to MB and GB.

4 posts

n0On3#

passing bytes to MB and GB.

I want to write bytes as MB and GB, but using this:
    IntCmpU $1 1000 "" "" "kB"
      FileWrite $0 "$4;$1Bytes;$1$\r$\n"
      Goto tornai
    kB:
    IntOp $2 $1 / 1024
    IntCmpU $2 1000 "" "" "MB"
      FileWrite $0 "$4;$2kB;$1$\r$\n"
      Goto tornai
    MB:
    IntOp $2 $2 / 1024
    IntCmpU $2 1000 "" "" "GB"
      FileWrite $0 "$4;$2MB;$1$\r$\n"
      Goto tornai
    GB:
    IntOp $2 $2 / 1024
    FileWrite $0 "$4;$2GB;$1$\r$\n"
    Goto tornai 
does a poor job (it writes 4888bytes as 4kB, at least it should write 5kB). I wanted to do it better but I don't get anything clear from IntFmt and that page from MSDN.

I wanted to write one decimal for MB and GB, but as we work with integers i don't know If nsis can do such a thing.


note: $4 is username, $2 is prefixed number, $1 is bytes.
kichik#
What you need is mod (%) not IntFmt.

Here is an example:
StrCpy $1 4888 # number of bytes
IntOp $2 $1 / 1024 # kilo-bytes
IntOp $3 $1 % 1024 # left over bytes
StrCpy $3 $3 1 # only one digit from the bytes
MessageBox MB_OK $2.$3KB # result in KB.B 
Same goes for MB and GB.
n0On3#edited
Are you sure that "mod" gives the decimal?

I think that mod gives the remainder and further operations must be done to get the decimals.

The problem is that when the decimal should be zero, nsis writes the next one.
n0On3#
ok, this is what I am finally using for passing Bytes to kB and MB. As GB usually make my nsis broke, I will take a look at them later.

$1 = bytes
$2 = with prefix

  IntCmp $1 1000 "" "" ">B"
  StrCpy $2 "$1Bytes"
  Goto SizeDone
  >B:
  IntOp $2 $1 / 1024
  IntCmp $2 1000 "" "" ">kB"
  StrCpy $2 "$2kB"
  Goto SizeDone
  >kB:
  IntOp $2 "$200" / 1024
  #DetailPrint "$4 Bytes: $1"
  #DetailPrint "$4 Result: $2"
  StrLen $7 $2
  IntOp $7 $7 - 1
  StrCpy $3 $2 1 $7 # copy last digit (second decimal)
  #DetailPrint "$4 Last digit: $3 (used for rounding)."
  IntCmp $3 4 "" "NoMBRounding"
  IntOp $2 $2 + 10
  #DetailPrint "$4 Rounded: $2 (forget last number)."
  NoMBRounding:
  IntOp $7 $7 - 1
  StrCpy $3 $2 1 $7 # copy one decimal
  StrCpy $2 $2 $7 # copy the integer part
  StrCpy $2 "$2.$3MB"
  SizeDone:
  DetailPrint "$2 = $1 bytes"