Page 1 of 1

x_val() whatever to dec procedure

Posted: Sun Jan 25, 2004 12:59 am
by blueznl
Code updated For 5.20+

simple straightforward and probably not too good but he, it works (i think) :-) and it might be usefull to some of us...

Code: Select all

Procedure x_val(string.s)
  Global x_val_type
  Protected p,l,b,t,c
  ;
  ; *** as normal val() except it accepts also &H &O &0 &B % \ $ 0X
  ;
  string = UCase(Trim(string))
  l = Len(string)
  t = 0
  ;
  If Left(string,1) = "$"
    p = 1
    b = 16
  ElseIf Left(string,1) = "%"
    p = 1
    b = 2
  ElseIf Left(string,1) = "\"
    p = 1
    b = 8
  ElseIf Left(string,1) = "&B"
    p = 2
    b = 2
  ElseIf Left(string,1) = "&O"
    p = 2
    b = 8
  ElseIf Left(string,1) = "&0"
    p = 2
    b = 8
  ElseIf Left(string,2) = "&H"
    p = 2
    b = 16
  ElseIf Left(string,2) = "0X"
    p = 2
    b = 16
    ;
    ; ElseIf Left(string,1) = "0"           ; i don't like this one, as i often use
    ;    p = 1                              ; preceding zeroes in front of decimals while
    ;    b = 8                              ; c(++) would turn those into octals... brrr...
    ;                                       ; well, it's up to you to uncomment these lines
  Else
    p = 0
    b = 10
  EndIf
  ;
  While p < l
    p = p+1
    c = Asc(Mid(string,p,1))-48
    If c > 9
      c = c - 7
    EndIf
    If c >= 0 And c < b
      t = t*b+c
    Else
      l = p
    EndIf
  Wend
  x_val_type = b
  ;
  ProcedureReturn t
EndProcedure