Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Wednesday, 19 November 2008

Converting Textual Data in a String to a Byte Array

What is the quickest and most efficient way of converting a string in VB6 to a byte array? This was a question I pondered recently when I saw various different ways of doing it, all of which turned out to be very inefficient.

First of all there is the most obvious way, loop through the string, character by character, converting each into its numerical character code and insert this into the byte array:

Dim strTemp As String Dim bytArray() As Byte Dim lngCount As Long ' If you have a string with a lot of data in it: strTemp = "Some long string..." ' This would be a very inefficient way ' of converting it to a byte array: ReDim bytArray(0 To Len(strTemp) - 1) For lngCount = 0 To Len(strTemp) - 1 bytArray(lngCount) = Asc(Mid(strTemp, lngCount + 1, 1)) Next

This is unfortunately very inefficient and I do not recommend you do it this way, ever. Even for very small strings.

Another way of doing it would be to instantiate the array by simply setting it to be equal to the string. The problem with this is that VB strings are always expressed as Unicode strings, therefore we would end up with a Unicode byte array and not an Ascii one.

If on the other hand you did want an Ascii byte array then you would have to remove the high byte from each character. Here is an example of how you could do this:

Dim strTemp As String Dim bytBuffer() As Byte Dim bytArray() As Byte Dim lngCount As Long strTemp = "Some long string..." ' Set a temporary array equal to the Unicode string. bytBuffer = strTemp ReDim bytArray(0 To Len(strTemp) - 1) For lngCount = 0 To UBound(bytBuffer) Step 2 ' Copy just the low byte of each character ' into the main byte array. bytArray(lngCount \ 2) = bytBuffer(lngCount) Next

Now this is much faster than the first method, however unless you have a specific need for a Unicode byte array (in which case you wouldn't need to copy the low byte of each character from a temporary array to the main array) there is an even faster and more reliable (and easier) way of converting a string to a byte array.

And that is to use the StrConv function:

bytArray = StrConv(strTemp, vbFromUnicode)

It's as simple as that. It's very easy, very reliable and very fast, you don't even need to initially dimension the array.

Monday, 3 November 2008

Checking For an Empty Array

I have seen many methods for dealing with arrays in VB6 that may contain no elements over the years. From using a counter variable to handling it in an error trap. But is there a correct way of handling arrays that have not been initialised and contain no data?

Yes! If you Not an array it will return -1 if it contains no elements. Here is an example:

If (Not variableArray) = -1 Then ' Array is empty. Else ' Array has elements. End If

And there you have it. No need to use a confusing error trap looking for a subscript out of range exception, no need to declare any extra variables and remembering to set them accordingly, just one line of code.

This works because Not is performing a bitwise operation on the array's 32 bit memory pointer. If this pointer is Null then all the bits get switched on and you end up with 0xFFFFFFFF, or -1 in VB6. If you Not it a second time you will end up with the array's memory pointer, or zero if it has not been initialised.


References:

  1. How do I check if array has been initialized?