Tech Tip: Utility Method to Convert Bytes
PRODUCT: 4D | VERSION: 15.x | PLATFORM: Mac & Win
Published On: January 18, 2018
Below is a utility method to convert Bytes to a larger denomination to reduce the number of digits displayed.
// Method: ByteConverter // // Details: // Convert Bytes to a larger denominator to reduce // the amount of characters displayed // // Parameters: // $1 - Longint value of the bytes // $2 - (Optional) Longint value of truncation, Defaults to 0 // // Output: // $0 - Text display of converted value with symbol // //------------------------------------------------------- C_TEXT($0;$result_t) C_LONGINT($1;$bytes_l) C_LONGINT($2;$truncPos_l) C_LONGINT($base_l) If(Count parameters>0) $bytes_l:=$1 If(Count parameters>1) $truncPos_l:=$2 Else $truncPos_l:=0 End if $base_l:=Trunc(Log($bytes_l)/Log(1024);0) $result_t:=String(Trunc($bytes_l/(1024^$base_l);2)) Case of : ($base_l=0) $result_t:=$result_t+" B" //Bytes : ($base_l=1) $result_t:=$result_t+" KB" //Kilobytes : ($base_l=2) $result_t:=$result_t+" MB" //Megabytes : ($base_l=3) $result_t:=$result_t+" GB" //Gigabytes End case $0:=$result_t End if |
Below is an example of the method in use:
C_LONGINT($inputByte_l) C_TEXT($result_t) $inputByte_l:=536870912 $result_t:=ByteConverter($inputByte_l) TRACE |
Results in: