Название (оригинал): Hex string & byte array conversions
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.Рус (машинный)эти функции преобразование шестнадцатеричных строк из/в байтовые массивы. Краткое Введение в каждой:
HexStringToBytes_A1: один из самых распространенных способов превратить шестнадцатеричной строки в строку байт, используя \”