View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Testing for a string of zero bytes

You mean you are looking for a string that consists purely of vbNullChar's ?

I'm sure reading the bytes into a byte array and testing
each one individually is NOT the best way


Think I'd do exactly that, looping a byte array is one of the fastest things
you can do in VBA
This does 2000 * 30 * 2 = 120k loops in a blink

Sub test()
Dim bNotAllZero As Boolean
Dim i As Long, j As Long
Dim s As String
Dim ba() As Byte

s = String(30, vbNullChar)
s = s & "a" ' uncomment for testing

ba = s

For j = 1 To 2000
For i = 0 To UBound(ba)
If ba(i) 0 Then
bNotAllZero = True
Exit For
End If
Next
Next
MsgBox bNotAllZero & vbCr & _
(j - 1) * (i) & " loops"

End Sub

Regards,
Peter T


"simonc" wrote in message
...
I am reading a series of bytes (around 2000) from a binary file and want
the
quickest way to test if all the bytes have a value zero. Would this be by
a
string comparison? I'm sure reading the bytes into a byte array and
testing
each one individually is NOT the best way. Incidentally I have to repeat
this
operation several hundred thousand times so any gain in speed will be
significant.

Grateful for advice.