Strip Leading Zeros

From NSIS Wiki

Description

Strings with a leading zero are treated as octal. So stripping zeros using IntOp won't work:

IntOp $v "00009" + 0

The Script

Name "TrimLeadingZeros"
OutFile "TrimLeadingZeros.exe"
ShowInstDetails show
 
 
var gen
 
Function TrimLeadingZerosCall
 
	Exch $R1
	Push $R2
Loop:
	StrCpy $R2 "$R1" 1
	StrCmp "$R2" "0" TrimLeft
	Goto Done
 
TrimLeft:
	StrCpy $R1 "$R1" "" 1
	Goto Loop
 
Done:
	StrCmp $R1 "" 0 +2
	StrCpy $R1 "0"
 
	Pop $R2
	Exch $R1
 
FunctionEnd
 
 
!macro TrimLeadingZeros NUM
	Push ${NUM}
	Call TrimLeadingZerosCall
	Pop ${NUM}
!macroend
 
!define TrimLeadingZeros `!insertmacro TrimLeadingZeros`
 
Section ""
 
	StrCpy $gen "00009"
	${TrimLeadingZeros} $gen
	DetailPrint "Already applied: |$gen|"
 
	StrCpy $gen "00000"
	${TrimLeadingZeros} $gen
	DetailPrint "Already applied: |$gen|"
 
SectionEnd

Output

  Already applied: |9|
  Already applied: |0|
  Completed
Personal tools
donate
ads