Remove leading zeros from string in Batch

sacsC
It is often need to convert string to int in batch. But:

set /a int=0100
echo Int: %int%

Will result

Int: 64

It ‘s because lead zero – seen as a HEX (0xNUMBER)

To solve use next:

@echo off
setlocal enableextensions enabledelayedexpansion

REM Removes leading "0" from string

REM Settings
set z_strtoint=%1

REM Init
set strterm=___ENDOFSTRING___
set tmp=%z_strtoint%%strterm%

REM Action
:loop
set char=%tmp:~0,1%
set tmp=%tmp:~1%
REM echo char: %char%
if [!char!] EQU [0] set z_strtoint=!z_strtoint:~1!
if [!char!] NEQ [0] goto endloop
if [!tmp!] NEQ [!strterm!] goto loop
:endloop

REM Show result
echo %z_strtoint%

So, exec:

z_strtoint 0100
100