Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 52
Default Testing for a string of zero bytes

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Testing for a string of zero bytes

Hi,

If InStr(mystring, "1") Then
MsgBox "contains ones"
Else
MsgBox "all zeroes"
End If

Mike

"simonc" wrote:

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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Testing for a string of zero bytes

Maybe a simple string comparison is faster. But it depends what you want to
do overall (eg other checking of the bytes), chances of a non-nullchar near
the beginning, etc. Try both methods with your actual data

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

s = String(2000, vbNullChar)
s2 = String(Len(s), vbnullcar)

's = s & "a" ' uncomment for testing

For i = 1 To 2000
bNotAllZero = s < s2
Next

MsgBox bNotAllZero
End Sub

Note I increased the length to 2000 as you say that's what you are dealing
with.

Regards,
Peter T


"Peter T" <peter_t@discussions wrote in message
...
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.





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Testing for a string of zero bytes

It all depends a bit how you have these bytes.
Are they in a string variable, in an array or are they still in a file?
Post the relevant code.

RBS

"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.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
testing a string for either "Minutes" and/or hours and extracting the values Bruce[_2_] Excel Programming 5 May 18th 07 08:05 PM
Testing same cell across multiple sheets for a string and counting each instance? [email protected] Excel Worksheet Functions 5 March 8th 07 02:57 PM
testing for a string type 6e Excel Programming 1 July 20th 06 06:02 PM
How many bytes? GARY Excel Discussion (Misc queries) 0 April 15th 06 09:31 PM
Testing a string array for any values Chris W. Excel Programming 9 March 21st 05 02:13 PM


All times are GMT +1. The time now is 03:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"