Asc: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 13: Line 13:
== Usage ==
== Usage ==
<highlight-nsis>
<highlight-nsis>
  ${Asc} "a"
  ${Asc} "a" $0 ; returns 97 in $0
Pop $0 ; returns 65
  ${Asc} "AA" $0 ; returns 65 in $0
  ${Asc} "bb"
Pop $0 ; returns 66
</highlight-nsis>
</highlight-nsis>


Line 25: Line 23:
;  
;  
; Syntax:
; Syntax:
; ${Asc} "String"
; ${Asc} "String" "Return"
;  
;  
; "String"  ; string argument
; "String"  ; string argument
;  
; "Return"  ; Variable where the ASCII code is returned. The function returns -1 if no ASCII code was found.
; Stack 1   ; Ascii code
;  
;  
; e.g.
; e.g.
; ${Asc} "a"
; ${Asc} "a" $0 ; returns 97 in $0
; Pop $0 ; returns 65
; ${Asc} "AA" $0 ; returns 65 in $0
; ${Asc} "bb"
; Pop $0 ; returns 66
;
;
     !define Asc `!insertmacro AscCall`
 
     !define Asc "!insertmacro Asc"
   
   
     !macro AscCall _STRING
     !macro Asc _STRING  
         ; save global registers
         ; save global registers
         Push $0
         Push $0
Line 52: Line 48:
         Push $9
         Push $9
         ; store parameter
         ; store parameter
         Push `${_STRING}`
         Push "${_STRING}"
         Call Asc
         Call Asc
         ; restore global register
         ; restore global register
Line 79: Line 75:
     StrCmp $0 256 0 Asc.Do
     StrCmp $0 256 0 Asc.Do
     StrCpy $0 "-1"
     StrCpy $0 "-1"


     Asc.Exit:
     Asc.Exit:

Revision as of 20:48, 16 July 2005

Author: nechai (talk, contrib)


Description

Returns an Integer representing the character code corresponding to the first letter in a string.

Syntax

${Asc} "String"

"String"   ; string argument

Stack 1    ; Ascii code

Usage

 ${Asc} "a" $0 ; returns 97 in $0
 ${Asc} "AA" $0 ; returns 65 in $0

The Function

Function Asc
; Returns an Integer representing the character code corresponding to the first letter in a string.
; 
; Syntax:
; ${Asc} "String" "Return"
; 
; "String"   ; string argument
; "Return"   ; Variable where the ASCII code is returned. The function returns -1 if no ASCII code was found.
; 
; e.g.
; ${Asc} "a" $0 ; returns 97 in $0
; ${Asc} "AA" $0 ; returns 65 in $0
;
 
    !define Asc "!insertmacro Asc"
 
    !macro Asc _STRING 
        ; save global registers
        Push $0
        Push $1
        Push $2
        Push $3
        Push $4
        Push $5
        Push $6
        Push $7
        Push $8
        Push $9
        ; store parameter
        Push "${_STRING}"
        Call Asc
        ; restore global register
        Pop $9
        Pop $8
        Pop $7
        Pop $6
        Pop $5
        Pop $4
        Pop $3
        Pop $2
        Pop $1
        Exch $0
    !macroend
 
    ; get parameter
    Pop $1 ; CHAR
    StrCpy $1 $1 1
 
    IntOp $0 0 + 0
    Asc.Do:
        IntFmt $2 %c $0
        System::Call 'kernel32::lstrcmpA(t, t) i(r1,r2) .r3'
        StrCmp $3 "0" Asc.Exit
        IntOp $0 $0 + 1
    StrCmp $0 256 0 Asc.Do
    StrCpy $0 "-1"
 
    Asc.Exit:
 
FunctionEnd