WBStudio.HexToDec
;----------------------------------------------------------------------------------------------------------------------
; Interactive Converter for Hex Numbers to Decimal Numbers.
; Hex Numbers must follow
; - the Visual Basic format '&H3&', '&H3D0', '"&H3&"' (enclosed in double quotes)
; - the C format  '0x0000', '0x00000000L'.
; For use in WinBatchStudio Editor only.
;----------------------------------------------------------------------------------------------------------------------
; For easy use just insert two lines into WSPOPUP.MNU, e.g.:
; _WBStudioHexToDec \ {F7}
;     Call("W:\WBT\UDF\WBStudioHexToDec.wbt","")
;
; Modify menu entry, filename, folderpath and hotkey to your needs.
;
; For undoing of replacements use standard hotkey Ctrl-Z
; or the symbol resp. item from WinBatch Studio menu.
;
; Search loop starts at current cursor position in forward direction.
;
; Detlev Dalitz.20020627.20020630.20020706.20020711.20020715
;----------------------------------------------------------------------------------------------------------------------

#DefineFunction udfHexToDec (hexstr)
HexChars = "0123456789ABCDEF"
hexstr = StrUpper(StrTrim(hexstr))
hexlen = StrLen(hexstr)
dec = 0
For x=1 To hexlen
   dec = (dec<<4)+StrIndex(HexChars,StrSub(hexstr,x,1),0,@FWDSCAN)-1
Next
Return (dec)
; Note: Returned negative numbers are ok.
#EndFunction


iCount = 0
If (RtStatus()<>10) Then Goto CANCEL ; In Studio or not?

p1 = '("*&[hH][0-9a-fA-F]+&*"*)' ; Visual Basic
p2 = '(0[xX][0-9a-fA-F]+[lL]*)'  ; C
p = StrCat(p1,"|",p2)

wFind("[ ""'`]",0,0,1,0)
While 1
   ; wFind (SearchText, Forward, MatchCase, Regex, Wrap)
   If !wFind(p,1,0,1,0) Then Break

   ; Get selection information.
   SelInfo = wSelInfo()
   ;SelStartLine = ItemExtract(1,SelInfo,@TAB)
   SelStartCol  = ItemExtract(2,SelInfo,@TAB)
   ;SelStopLine  = ItemExtract(3,SelInfo,@TAB)
   SelStopCol   = ItemExtract(4,SelInfo,@TAB)

   ; Check if number is comment formatted e.g. "; &H3D0"
   ; If so then skip and continue the search loop.
   wEdGoToCol(SelStartCol-1)
   If (wGetChar() == " ")
      wEdLeft()
      If (wGetChar() == ";")
         wEdGoToCol(SelStopCol)
         Continue
      EndIf
   EndIf

   ; Get the selection.
   wEdGoToCol(SelStartCol)
   wEdStartSel()
   wEdGoToCol(SelStopCol)
   wEdEndSel()
   sGet = wEdGetWord()

   ; Compute the selection and replace it.
   sPut = StrUpper(sGet)
   sPut = StrClean(sPut,"0123456789ABCDEF","",@TRUE,2)
   sPut = udfHexToDec(sPut)
   iPutLength = StrLen(sPut)
   wEdInsString(sPut)

   ; Append original number as comment at end of current line.
   wEdEnd()
   wEdInsString(" ; ")
   wEdInsString(sGet)

   ; Reposition the cursor.
   wEdGoToCol(SelStartCol+iPutLength)

   ; Make the cursor visible.
   wEdClearSel()

   ; Increment counter.
   iCount = iCount  + 1

   ; Ask for more.
   Pause("WBStudioHexToDec","Search and Replace again?")

EndWhile


:CANCEL
If (iCount > 0) Then Message("WBStudioHexToDec",StrCat("Replaced items: ",iCount))

If (IntControl(77,80,0,0,0) > 0) Then Return
Exit

;----------------------------------------------------------------------------------------------------------------------



; --- test  area ---

"&H80000000"
"&H3D0" "&H3D0"
"&H3&"
&H80000000  &H3D0  &H3D0  &H3&

0x80000000
0x3d0      0x3d0
0x3
0x80000000L  0x3D0   0x3D0   0x3

;----------------------------------------------------------------------------------------------------------------------
;*EOF*