Results 1 to 4 of 4

Thread: Can you write this better?

  1. #1

    Thread Starter
    Hyperactive Member Alan777's Avatar
    Join Date
    Jan 2001
    Location
    New Zealand
    Posts
    303

    Question

    I need to write a lot of Long values to a file fast.
    If I use Write, a Long value will use 8 characters in the
    file. I have prepared a module to convert a Long value
    to a 3 character string for writing to a file (and convert it back again).
    Is this ok? It works fine but is there a better, less messy way?
    Also I don't want to use binary mode as it is painfully slow.
    Code:
    Public Function ThreeByteStringFromLong(lngNumber As Long) As String
     ThreeByteStringFromLong = Chr(R(lngNumber)) + Chr(G(lngNumber)) + Chr(B(lngNumber))
    End Function
    
    Public Function LongFromThreeByteString(str3Byte As String) As Long
     Dim strA As String
     Dim strB As String
     Dim strC As String
     strA = Left(str3Byte, 1)
     strB = Mid(str3Byte, 2, 1)
     strC = Right(str3Byte, 1)
     LongFromThreeByteString = RGB(Val(strA), Val(strB), Val(strC))
    End Function
    
    Private Function R(lColor As Long) As Byte
     R = lColor And &HFF&
    End Function
    
    Private Function G(lColor As Long) As Byte
     G = Int(lColor / &H100&) And &HFF&
    End Function
    
    Private Function B(lColor As Long) As Byte
     B = Int(lColor / &H10000) And &HFF&
    End Function
    "Today's mighty oak is just yesterday's nut,
    that held its ground."

  2. #2
    Dreamlax
    Guest
    Binary? Slow? Maybe I'm getting confused now.

  3. #3

    Thread Starter
    Hyperactive Member Alan777's Avatar
    Join Date
    Jan 2001
    Location
    New Zealand
    Posts
    303
    Binary? Slow? Maybe I'm getting confused now.
    Well, when you use binary mode for file access, you are processing the file byte by byte right? Whereas if you can use, say Line Input, that is a heck of a lot faster.
    I did something else like this once before to test the theory, and while I was able to make the file 8 times smaller using binary mode, it also took 8 times longer to process the data.
    "Today's mighty oak is just yesterday's nut,
    that held its ground."

  4. #4

    Thread Starter
    Hyperactive Member Alan777's Avatar
    Join Date
    Jan 2001
    Location
    New Zealand
    Posts
    303
    To me, binary mode is only good if you need to manipulate a few bytes of a file and you know exactly where they are in the file.
    "Today's mighty oak is just yesterday's nut,
    that held its ground."

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