Page 1 of 1
Converting from bytes to gigabytes in multiple division statements.
Posted: 10 Jan 2023 14:54
by BoQsc
Note: 30417637376 Bytes = 28.3286 Gigabytes
Let's say I want to convert
30417637376 of bytes to gigabytes.
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.
Re: Converting from bytes to gigabytes in multiple division statements.
Posted: 10 Jan 2023 15:21
by Joe Caverly
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
Re: Converting from bytes to gigabytes in multiple division statements.
Posted: 10 Jan 2023 15:36
by Joe Caverly
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
Re: Converting from bytes to gigabytes in multiple division statements.
Posted: 10 Jan 2023 19:01
by Aacini
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.
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