Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Converting long integer into date
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
01054948
Message ID:
01055157
Views:
15
>Hi all,
>
>I have a problem convertin data read from a binary file. I have to import these data into a DBF.
>
>Data in the source file where stored by an old DOS based application (may be Pascal or Basic) I know the structure of this file but I have some trouble converting numeric data.
>
>For exemple Date are stored in YYYYMMDD format, as a long (4 bytes)
>
>Exemple :
> ASC(byte1) = 56
> ASC(byte2) = 203
> ASC(byte3) = 49
> ASC(byte4) = 1
>
>This should be converted to a date.... but HOW ??????
>
>Thanks for any answer

Sergey's response only can be applied to VFP 9.0. Before that, CTOBIN() did take only one parameter.

While I know you've solved the problem, here's some code that might be useful in the future.
lcdate = CHR(56) + CHR(203) + CHR(49) + CHR(1)
SET PROCEDURE TO Con_Int
oConvert = CREATEOBJECT('ConvertInt')
? TRANSFORM(oConvert.StringToInteger(lcdate))
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
George

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

Click here to load this message in the networking platform