VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Hex string & byte array conversions

by Vesa Piittinen (20 Submissions)
Category: String Manipulation
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

These functions convert hex strings from/to byte arrays. A quick introduction to each:



  • HexStringToBytes_A1: one of the most common ways to turn hex string to byte string using "&H" string conversion to a byte value.

  • HexStringToBytes_A2: an optimized version of A1, grabs bigger values in one go and reduces amount of strings created.

  • HexStringToBytes_A1: fills byte array using AscW & MidB$

  • HexStringToBytes_A2: same as before, but uses the passed string as a buffer instead just for comparison. Slower as creates more strings than A1.

  • HexStringToBytes_C1: uses Windows CryptStringToBinary API.

  • HexStringToBytes_S1: uses a lot of advanced VB6 tricks to achieve the greatest speed. API just can't compete with this one.

  • HexStringToBytes_F1: uses advanced VB6 tricks like last one, but allows for any formatting in the passed hex string (such as spaces, line changes or anything else – simply looks for any valid hex pairs it can find).

Shortly put: the C1 and F1 functions can parse "41 00 42 00 43 00", with formatting. The others only accept "410042004300", no formatting. Those hex strings would convert into a byte array:



  • B(0) = 65

  • B(1) = 0

  • B(2) = 66

  • B(3) = 0

  • B(4) = 67

  • B(5) = 0


Or "ABC" as a string (Dim S As String: S = B).


There are also functions working the other way around, BytesToHexString_C1 & BytesToHexString_F1. C1 is a version using CryptBinaryToString API while F1 is an optimized function that allows for custom output formatting! You can have any kind of formatting: none at all, just spaces between each hex pair, HTML, XML, C notation, JSON... use your imagination.


Not only that, the F1 version is way faster than the C1 version with the exact same output!


The following tricks are generally used for speed in the S1 and F1 functions:



  • Safe arrays: VB6 stores arrays internally as safe arrays. The function creates "fake" Long and Integer arrays. Integer array for accessing the string data (1 character = 2 bytes = Integer) and a Long array for memory manipulation (PutMem4 & GetMem4 replacement) as well as saving the decoded hex string to the byte array.

  • SysAllocStringByteLen: this API creates a BSTR and it does that very fast, especially if pointer is set to 0, in which case you get any free point from memory. That memory is not touched in any way except for setting up the minimal BSTR structure information (4 bytes string length & 2 bytes null terminator). This memory is not given to a string however! Instead it is given to the resulting Byte array. Why? It saves an unnecessary step of nullifying all the bytes that are reserved, and this always happens if you just ReDim an array.


Long is the fastest datatype in VB6 in current processors. This is why a Long array is used to store the new bytes into the Byte array instead of just using it directly.


Summa summarum: these functions should give good knowledge for anyone who wants to understand how memory can be handled so that you achieve better performance code in VB6. It isn't pretty though.

Rate Hex string & byte array conversions

Download Hex string & byte array conversions

Download Hex string & byte array conversions (11 KB)

Hex string & byte array conversions Comments

No comments have been posted about Hex string & byte array conversions. Why not be the first to post a comment about Hex string & byte array conversions.

Post your comment

Subject:
Message:
0/1000 characters