中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-12 11:47
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Invalid numeric comparison operations under if
Printable Version  2,983 / 17
Floor1 Hanyeguxing Posted 2009-10-03 21:22
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
It can be summarized like this: when comparing invalid octal or hexadecimal numbers:

1. When the string to be compared does not contain "-": when comparing an invalid number with a string that does not contain "-", character comparison is performed.
2. When the first character of the string to be compared is only "-": if the invalid number is the same as the characters after "-", the invalid number is smaller.
3. When the string to be compared contains "-" and is not the above situation: compare the invalid number with the string obtained by removing all "-" (allowing the result after removing all "-" to be empty) character by character.




I think this example should best illustrate the problem of "-" being removed and all "-" being removed during the comparison of invalid numbers.

[ Last edited by Hanyeguxing on 2009-10-6 at 20:03 ]
Floor2 netbenton Posted 2009-10-03 23:25
银牌会员 Posts 752 Credits 1,916 From 广西
It should be in violation of the digital rules, so it is compared as a string. Because "-" is after "0", so 08 is less than -1.
Floor3 Hanyeguxing Posted 2009-10-04 12:04
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
if 08 gtr - echo 08 greater than -
if 08 lss -1 echo 08 less than -1
But the strange thing is, when comparing 08 with -, 0 is greater than -; while comparing 08 with -1, 0 is less than -. It's very strange, I don't understand why this is happening?
Floor4 523066680 Posted 2009-10-04 19:06
银牌会员 Posts 1,133 Credits 2,362
When comparing strings, it starts from the first character and goes until the first different one appears.

Hey~ What the person above said... Could it be that one is treated as a number and the other as a string...

[ Last edited by 523066680 on 2009-10-4 at 19:09 ]
Floor5 wxcute Posted 2009-10-04 19:10
中级用户 Posts 211 Credits 458
A good discovery. It seems that CMD is not generally easy to understand.
Floor6 plp626 Posted 2009-10-06 00:06
银牌会员 Posts 1,020 Credits 2,278
08 is not a legal number, and cmd treats it as a string
Floor7 HAT Posted 2009-10-06 09:31
版主 Posts 5,017 Credits 9,023
有时间就多看看帮助
Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.
Floor8 Hanyeguxing Posted 2009-10-06 10:03
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
Originally posted by HAT at 2009-10-6 09:31:
Read the help more when you have time.
Numeric values are decimal numbers, unless
prefixed by 0x for hexadecimal numbers, and 0 for octal numbers.
So 0x12 is the same as 18 is the same as 022. Please note that the octal
notation can be confusing: 08 and 09 are not valid numbers because 8 and
9 are not valid octal digits.

I know that 08 is not a legal number, but under if, it can be compared character-wise. It's just that there are problems when comparing with - and -0.
Floor9 netbenton Posted 2009-10-06 13:06
银牌会员 Posts 752 Credits 1,916 From 广西
It's figured out!

if 08 lss -1 echo 08 is less than -1

In this case, it should be a string comparison, and just the "-" sign in -1 is discarded. If the character after "-" is larger than 0, then it's larger

if 08 lss -# echo 08 is less than -#
if 08 lss -07 echo 08 is less than -07
if 08 lss -09 echo 08 is less than -09

But it can't be equal:
if 08 equ -08 echo 08 equals -08

This should be a bug in the processing of the if statement by cmd.exe.
However, sometimes these bugs can be exploited
Floor10 plp626 Posted 2009-10-06 13:41
银牌会员 Posts 1,020 Credits 2,278
Originally posted by Hanyeguxing at 2009-10-4 12:04 PM:
if 08 gtr - echo 08 greater than -
if 08 lss -1 echo 08 less than -1
But strangely, when comparing 08 with -, 0 is greater than -; but when comparing 08 with -1, 0 is less than -. It's very strange, I don't understand why this is the case?


Sorry, my previous post didn't address the owner's question, I just saw it now.

Maybe it's a bug...
Floor11 Hanyeguxing Posted 2009-10-06 13:50
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
Solved, thank you very much netbenton!

[ Last edited by Hanyeguxing on 2009-10-6 at 15:40 ]
Floor12 gool123456 Posted 2010-04-22 20:52
初级用户 Posts 76 Credits 89
Originally posted by netbenton at 2009-10-6 13:06:
Got it!

if 08 lss -1 echo 08 is less than -1

This situation should be a string comparison. Only the "-" sign in -1 is discarded. If the characters after "-" are larger than 0, it is larger.

if 08 lss ...


I think IF is bit comparison.
You can use the following IF statements to test:

if 08 lss -0# echo 08 is less than -#
if 008 lss -07 echo 08 is less than -07
if 8 lss -09 echo 08 is less than -09
pause

Then test the following:
if 08#0 lss -9#0 echo 08 is less than -#
if 008 lss -07 echo 08 is less than -07

if 8 lss -09 echo 08 is less than -09
pause

The following is my analysis:
When IF encounters a string comparison of both numbers and characters for size, because it cannot compare the size, it shifts backward at the same time to take numbers for comparison, replacing the original bit, just like the SHIFT command, taking parameters! It continues until all are numbers, then makes the comparison. The larger one is larger.
Floor13 Hanyeguxing Posted 2010-04-22 21:01
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
if a1 equals b1 echo. Is it the same
Floor14 Hanyeguxing Posted 2010-04-22 21:02
银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂
Originally posted by gool123456 at 2010-4-22 20:52:
The following is my analysis:
When IF encounters a string comparison of numbers and characters at the same time, since it cannot compare the size, it shifts backward at the same time to take numbers for comparison, replacing the original bit, just like the SHIFT command, taking parameters! Until both are numbers, then compare. The larger one is larger....


Floor15 gool123456 Posted 2010-04-22 21:19
初级用户 Posts 76 Credits 89
The EQU of if should be for string comparison, and the sizes are different.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023