letter strings return the values as number strings problem

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

letter strings return the values as number strings problem

#1 Post by nnnmmm » 27 Aug 2017 09:07

Code: Select all

SET S=2A
SET /A B1=2/S
ECHO %B1%
1

SET /A S=2A
SET /A B2=2/S
ECHO %B2%
1

SET /A S="D 2"
SET /A B3=2/S
ECHO %B3%
NONE

SET /A S="2 D"
SET /A B4=2/S
ECHO %B4%
1

I cant understand strings here, letter strings return the values as number strings.

how do you intercept the missing operator ones before they get evaluated if letter strings are number strings?

and |findstr method treats number strings as letter strings , so they do not understand the negative numbers.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: letter strings return the values as number strings problem

#2 Post by aGerman » 27 Aug 2017 10:11

No idea what you're trying to do here.

Basically Batch knows only one variable type that is, string of characters. There are commands that trigger an internal conversion from strings to 32 bits wide signed integer (such as SET /A).
- If a string consists of digits (with maybe a plus or minus sign prepended) and is in the range of signed integers then it can be converted. E.g. -12345.
- If such a numeric string is prepended with a 0 then a conversion as octal number is triggered. E.g. 012345 (that is decimal 5349). Digits 8 and 9 are invalid in octal numbers.
- If a string is prepended by 0x the following characters will be converted as hexadecimal number. E.g.
0x12345 (that is decimal 74565). A-F (upper or lowercase) are valid hex digits.
- If a string is a variable name of a variable containing a valid numeric string then the variable will be automatically expanded using SET /A.

Note that always integer calculations are performed. There is no floating point type in Batch.

Steffen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: letter strings return the values as number strings problem

#3 Post by dbenham » 27 Aug 2017 14:14

I've documented how SET /A interprets variables with invalid numeric content as part of my Rules for how CMD.EXE parses numbers post.
dbenham wrote:SET /A Variables (all versions)

The rules for parsing un-expanded numeric variables are different. All three numeric notations employ a similar strategy: First ignore any leading negative sign and convert the number into an unsigned binary representation, stopping as soon as an invalid character is reached. Then apply any leading negative sign by taking the 2's compliment.

The big difference is that overflow conditions no longer result in an error. Instead the maximum magnitude value is used. A positive overflow becomes 2147483647, and a negative overflow becomes -2147483648.

Undefined variables are treated as zero, and variables that do not contain a valid numeric format are treated as zero.

A defined variable that does not start out as a valid number is treated the same as an undefined variable - value equals zero. But something like -123JUNK is assigned value -123.


Dave Benham

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

Re: letter strings return the values as number strings problem

#4 Post by nnnmmm » 27 Aug 2017 21:00

i needed to find if a string was a number string, if it was a number, then i needed to make 1 measure, if it is not then i need to make 2nd measure. if a filename was created with a certain number at certain position of the file name then i must not rename it.

so i tried to make "divided by 0" so its errorlevel could return with some value that identified as a number string, but it failed, a "2d" letter string was returned as a number string 2. so i had to investigate SET /A itself.

so in your saying, i have to check its base from a letter string of a filename if it is a binary or octal or decimal or hex.. etc...to make sure...

i think i am about to faint.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: letter strings return the values as number strings problem

#5 Post by aGerman » 28 Aug 2017 00:56

i think i am about to faint.

So do we because you are still not able to discribe your problem. You just try telling us something about "number strings" and "letter strings" instead.
Out of your latest post I learned it's all about file names. So what does "number string" mean? Do the file names consist of digits only like 0123456.txt? Or does the file name contain digits like foo0123456bar.txt? Or is the numeric part separated from the rest by a delimiter like foo_0123456.txt?
Just provide real examples and explain what your final goal is.
How to get help for a batch script - quickly!

Steffen

Post Reply