When using SET /A for operations, if the first digit of the character is zero and the value contains 8 or 9, it will cause the value to not be read correctly.
Because except that hexadecimal has the 0x prefix and octal has the 0 prefix, the numeric value is in decimal. Therefore, 0x12 is the same as 18 and 022. Please note that octal formulas can be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.
Example:
C:\>set /a minute=08+5
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=5+018
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=0029+8
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=125+09
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
Solutions:
1. When operating constants, if the constant contains 8 or 9, do not add zero in front, in short, the highest digit cannot be zero.
Example: Incorrect (08, 009, 018, 0029)
Correct (8, 9, 18, 29)
2. Prevent the highest digit of the variable from being zero during variable operation;
Algorithm:
First take the first digit of the variable and judge whether it is zero;
If it is true, then take the remainder after the first digit and reassign it to the variable;
Execute the loop until the first digit is not zero;
Finally, use the zero-removed variable for operation.
Code:
@echo off
set mu=0001028
echo Before removing zero=%mu%
:again
if "%mu:~0,1%"=="0" (
set mu=%mu:~1%
goto again
)
echo After removing zero=%mu%
set /a num=%mu%+154
echo Operation result=%num%
pause
I encountered this problem when writing a batch script today, and finally figured out the reason.
Also found the solution, and in the future, everyone can avoid detours when encountering corresponding problems.
[ Last edited by pengfei on 2006-10-2 at 09:41 ]
Because except that hexadecimal has the 0x prefix and octal has the 0 prefix, the numeric value is in decimal. Therefore, 0x12 is the same as 18 and 022. Please note that octal formulas can be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.
Example:
C:\>set /a minute=08+5
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=5+018
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=0029+8
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
C:\>set /a minute=125+09
Invalid number. Numeric constants can only be decimal (17), hexadecimal (0x11) or octal (021).
Solutions:
1. When operating constants, if the constant contains 8 or 9, do not add zero in front, in short, the highest digit cannot be zero.
Example: Incorrect (08, 009, 018, 0029)
Correct (8, 9, 18, 29)
2. Prevent the highest digit of the variable from being zero during variable operation;
Algorithm:
First take the first digit of the variable and judge whether it is zero;
If it is true, then take the remainder after the first digit and reassign it to the variable;
Execute the loop until the first digit is not zero;
Finally, use the zero-removed variable for operation.
Code:
@echo off
set mu=0001028
echo Before removing zero=%mu%
:again
if "%mu:~0,1%"=="0" (
set mu=%mu:~1%
goto again
)
echo After removing zero=%mu%
set /a num=%mu%+154
echo Operation result=%num%
pause
I encountered this problem when writing a batch script today, and finally figured out the reason.
Also found the solution, and in the future, everyone can avoid detours when encountering corresponding problems.
[ Last edited by pengfei on 2006-10-2 at 09:41 ]

