Converting from bytes to gigabytes in multiple division statements.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Converting from bytes to gigabytes in multiple division statements.

#1 Post by BoQsc » 10 Jan 2023 14:54

Note: 30417637376 Bytes = 28.3286 Gigabytes
Let's say I want to convert 30417637376 of bytes to gigabytes.

Code: Select all

set /A "gb=30417637376/1024"
The usual issue is that Command Prompt will refuse to do the division with such large number.

Code: Select all

C:\Users\Windows10>set /A "gb=30433865728/1024"
Invalid number.  Numbers are limited to 32-bits of precision.
Are anyone aware of a simple working solution to accurately convert Bytes to Gigabytes in Batch language?

Currently I'm not really good at math, but maybe Converting from bytes to
gigabytes in multiple division statements would retain accuracy.

Not sure about that.

Joe Caverly
Posts: 18
Joined: 11 Jul 2018 05:05

Re: Converting from bytes to gigabytes in multiple division statements.

#2 Post by Joe Caverly » 10 Jan 2023 15:21

Rob van der Woude has a page that explains Math in NT Batch Files
https://www.robvanderwoude.com/battech_math.php.

There is a section on that page titled "Workarounds: 32-bit"

Rob offers three workarounds for your problem.

Joe

Joe Caverly
Posts: 18
Joined: 11 Jul 2018 05:05

Re: Converting from bytes to gigabytes in multiple division statements.

#3 Post by Joe Caverly » 10 Jan 2023 15:36

I have found a batch file to do what you want.

Code: Select all

R:\>str_math.bat 30417637376 / 1024
29704724
It is available via PasteBin;
https://pastebin.com/hrDVeeU1

Note that the result is not instantaneous.

Please advise if this solution solves your problem.

Joe

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Converting from bytes to gigabytes in multiple division statements.

#4 Post by Aacini » 10 Jan 2023 19:01

BoQsc wrote:
10 Jan 2023 14:54
Note: 30417637376 Bytes = 28.3286 Gigabytes
Let's say I want to convert 30417637376 of bytes to gigabytes.

Code: Select all

set /A "gb=30417637376/1024"
The usual issue is that Command Prompt will refuse to do the division with such large number.

Code: Select all

C:\Users\Windows10>set /A "gb=30433865728/1024"
Invalid number.  Numbers are limited to 32-bits of precision.
Are anyone aware of a simple working solution to accurately convert Bytes to Gigabytes in Batch language?

Currently I'm not really good at math, but maybe Converting from bytes to
gigabytes in multiple division statements would retain accuracy.

Not sure about that.
Yes, you are right. I taken the conversion program posted at this answer and modified it a little in order to fulfill your specific request. Here it is:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem DecimalToPowerOf1024.bat bigNumber [arrayName]
rem Decimal to power-of-1024 base conversion of an unlimited size decimal number 
rem Antonio Perez Ayala

rem Get the name of the array, if any
set power=PowerOf1024
if "%~2" neq "" set "power=%~2"

rem Divide the number in 9-digits groups, eliminating left zeros in each group
set number=%1
set groups=0
:nextGroup
   set group=%number:~-9%
   for /L %%a in (1,1,8) do if "!group:~0,1!" equ "0" set group=!group:~1!
   set /A groups+=1
   set group[%groups%]=%group%
   set number=%number:~0,-9%
if defined number goto nextGroup

rem Convert the 9-digits groups to power-of-1024 values
set /A bitPos=0, %power%=0, %power%[0]=0
:nextBinaryDigit
   rem Divide the 9-digits groups by 2 
   set carry=0
   for /L %%i in (%groups%,-1,1) do (
      set /A term=carry*1000000000+group[%%i], group[%%i]=term/2, carry=term%%2
   )
   rem Insert remainder in current PowerOf1024 value, in right-to-left order
   set /A "%power%[!%power%!]+=carry<<bitPos, bitPos+=1"
   rem If current PowerOf1024 value completed: pass to next one
   if %bitPos% equ 10 set /A bitPos=0, %power%+=1 & set %power%[!%power%!]=0
   rem If last (most significant) group was completely converted: eliminate it
   if !group[%groups%]! equ 0 set /A groups-=1
   rem And pass to convert the rest of 9-digits groups
if %groups% gtr 0 goto nextBinaryDigit
for %%p in (!%power%!) do if !%power%[%%p]! equ 0 set "%power%[%%p]=" & set /A %power%-=1

set %power%

REM Second part: show the result in GB with 4 decimals

set /A "fractionOfGB=(PowerOf1024[2]*1024+PowerOf1024[1])*100/1024*100/1024"
set "decimal=0000%fractionOfGB%"
echo/
echo %1 Bytes = %PowerOf1024[3]%.%decimal:~-4% Gigabytes
Output example:

Code: Select all

C:\> test 30417637376
PowerOf1024=3
PowerOf1024[0]=0
PowerOf1024[1]=532
PowerOf1024[2]=336
PowerOf1024[3]=28

30417637376 Bytes = 28.3286 Gigabytes
Antonio

Post Reply