Results 1 to 3 of 3

Thread: 4 Byte int to text file

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Galway, Ireland
    Posts
    316
    I have to read a 4 byte Int from a text file add one on and write it back.

    I have a text file with 1 record which is 512 bytes long
    byte 257,258,259,260 is an int

    That 4 byte field is a binary intel format integer, in standard
    intel layout (i.e low byte, high byte, low byte, high byte). So for example
    - a 93 decimal would be stored as:
    0x5D 0x00 0x00 0x00

    how can I read this add one and write it back
    have triwd the following with no success (there are other ints and hex etc in this file it is not all ascii!!
    (this int is onlt between 1 and 999 rolling)

    I cannot change the layout of the file as it is in use by another application that I cannot change.


    Open "TEST.TXT" For Input As #1
    Dim byte1 As Integer
    Dim byte2 As Integer
    Dim byte3 As Integer
    Dim byte4 As Integer
    Dim t_wsr As Integer

    Line Input #1, linedata
    byte1 = Asc(Mid(linedata, 257, 1))
    byte3 = Asc(Mid(linedata, 259, 1)) * 256
    t_wsr = byte1 + byte2
    t_wsr = t_wsr + 1
    If t_wsr = 1000 Then
    t_wsr = 1
    End If
    byte1 = t_wsr Mod 256
    byte3 = Int((t_wsr Mod 65536) / 256)
    byte2 = &H0
    byte4 = &H0

    Close #1
    Open remote_drive & file_name For Output As #1
    Print #1, Left(linedata, 257) & Chr$(byte1) & Chr$(byte2) & Chr$(byte3) & Chr$(byte4) & Space(251)
    Close #1


    Help Please

  2. #2
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722
    Try opening the file as binary.
    Code:
    Dim byte(0 to 3) as byte
    Dim i as integer
    Open "TEST.TXT" for binary as #1
    For i = 0 to 3
        Get #1, 257 + i, byte(i)
        ' Get #<filenumber>, <startnumber>, <variable>
    Next
    The Array byte() will now hold each of the 4 bytes
    Code:
        Dim strHex as String
        strHex = Hex(byte(0))
        'strHex wil be 5D if 93 dec was stored

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Galway, Ireland
    Posts
    316
    That worked fine for getting it but how do I add 1 decimal and then write back tried this but it doesn't seem to change the data??

    Open remote_drive & file_name For Binary As #1
    For i = 0 To 3
    Get #1, 258 + i, t_byte(i)
    Next i
    t_wsr = Hex(t_byte(0)) + (Hex(t_byte(2)) * 256)
    t_wsr = t_wsr + 1
    If t_wsr >= 1000 Then
    t_wsr = 1
    End If
    t_byte(0) = CByte((t_wsr Mod 256))
    t_byte(1) = CByte(0)
    t_byte(2) = CByte((Int((t_wsr Mod 65536) / 256)))
    t_byte(3) = CByte(0)

    For i = 0 To 3
    Put #1, 258 + i, CByte(Hex(t_byte(i)))
    Next i

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width