Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting a String to Integer or Long
Message
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Miscellaneous
Thread ID:
00147505
Message ID:
00147529
Views:
22
Hi Dennis,

Converting a signed number to a string really isn't a problem. Here's a class I've written that handles it, with a syntax example following:
DEFINE CLASS ConvertInt AS CUSTOM

  FUNCTION IntegerToString
    
    LPARAMETER pnInteger, pnbytes
    
    LOCAL lcresult, lnbytes, lnmask,;
      lninteger, lni, lnchar
    lcresult = ""
    IF PCOUNT() = 2
      lnbytes = pnbytes
    ELSE
      * Default to DWORD
      lnbytes = 4
    ENDIF
    lninteger = pnInteger
    lnmask = 255
    FOR lni = 1 TO lnbytes
      lnchar = BITAND(lninteger, lnmask)
      lcresult = lcresult + CHR(lnchar)
      lninteger = BITRSHIFT(lninteger, 8)
    NEXT
    RETURN lcresult
  ENDFUNC
  
  FUNCTION StringToInteger
    
    LPARAMETER pcstring, plsigned
    
    LOCAL lnresult, lnlast, lni, llsigned,;
      lnmsb, lnmax
    lnresult = 0
    lnlast = LEN(pcstring)
    * Return Signed Integer?
    IF PCOUNT() = 2
      llsigned = plsigned
    ELSE
      llsigned = .F.
    ENDIF
    FOR lni = 1 TO lnlast
      lnresult = lnresult + ASC(SUBSTR(pcstring, lni, 1)) * (256 ^ (lni - 1))
    NEXT
    IF llsigned
      lnmsb = (lnlast * 8) - 1
      IF BITTEST(lnresult, lnmsb)
        lnmax = (2 ^ (lnmsb + 1))
        lnresult = lnresult - lnmax
      ENDIF
    ENDIF
    RETURN lnresult
  ENDFUNC
ENDDEFINE
* Should set procedure to filename first
oInts = CREATEOBECT("ConvertInt")
lcvalue = oInts.IntegerToString(-1) && will return a four character string
? oInts.StringToInteger(lcvalue, .T.) && .T. indicates a signed integer will display -1.00
hth,
George

Ubi caritas et amor, deus ibi est
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform