Doing cryptographic operations in Classic Visual Basic 6 and VBA is tricky because VB6 doesn'thave unsigned types (except the Byte type).That da*n sign bit keeps messing things up. A simple operation in C likex<<=8
is not a simple matter with a Long variable in VB6.Try this experiment:
- Vb6 Read Binary File Into Byte Array C
- Vb6 Read Binary File Into Byte Array
- Vb6 Dynamic Array
- Vb6 Read Binary File Into Byte Array Software
String
Jul 03, 2017 Dim docContent As Byte = File.ReadAllBytes(path:=CachedFilePath) I am trying to read binary files into a Byte array. I get a 'System.OutOfMemoryException'. I believe that I need to setup a loop and write chunks to the byte array. Some vb code would be greatly appreciated. If the File Download dialog box appears, do one of the following: To start the download immediately, click Open. To copy the download to your computer to view at a later time, click Save.
typesandByte
arrays, use the built-in VBAStrConv()

Note how the StrConv
function avoids the need to declare the size of the byte array beforehand.(Thanks to Robert Garofalo for pointing out this useful function).For more information see Using StrConv with ANSI, DBCS and Unicode charactersets.
If you want to convert to a byte array without the Unicode conversion,do this:
This will produce the result:This time, we get two bytes for each Unicode character.See Microsoft Knowledge Base Article - 187675 HOWTO: Copy a String to a Byte Array Without Unicode ConversionVb6 Read Binary File Into Byte Array C
for more details.If you want the string encoded in UTF-8, seeHow to convert VBA/VB6 Unicode strings to UTF-8.
VB6/VBA Code
The functions provided here inbasConvert andbasUnsignedWordcan help you manage some of these issues in your code.Thanks to Ernie Gibbs for pointing out a subtle error in basUnsignedWord.uw_WordAdd
(2008-06-25).The code in basConvert replaces the now supersededbasByteUtilswith better and faster dynamic arrays.However, the functions in basByteUtils are still useful for ASP and VBScript applications.The source code to these three files is in binaryutils.vb6.zip (9 kB).
Note: Users with operating systems that are set up to use full 32-bit Unicode OS or oriental CJK charactersmay need to do a global replace of Asc() and Chr() with AscW() and ChrW() in the basConvertand basByteUtils functions above. Thanksto David Wolf of Intuit Information Technology Solutions for this tip.
For more hints on how to use arrays of the Byte type in Visual Basic 6 compared to thesimpler String type, see Using Byte Arrays in VB6/VBA.
Related Topics
See also our pages on:Contact
For more information or to comment on this page, please send us a message.
This page last updated 12 December 2019
Vb6 Read Binary File Into Byte Array
-->By default, the DataReader loads incoming data as a row as soon as an entire row of data is available. Binary large objects (BLOBs) need different treatment, however, because they can contain gigabytes of data that cannot be contained in a single row. The Command.ExecuteReader method has an overload that will take a CommandBehavior argument to modify the default behavior of the DataReader. You can pass SequentialAccess to the ExecuteReader method to modify the default behavior of the DataReader so that instead of loading rows of data, it will load data sequentially as it is received. This is ideal for loading BLOBs or other large data structures. Note that this behavior may depend on your data source. For example, returning a BLOB from Microsoft Access will load the entire BLOB being loaded into memory, rather than sequentially as it is received.
Vb6 Dynamic Array
When setting the DataReader to use SequentialAccess, it is important to note the sequence in which you access the fields returned. The default behavior of the DataReader, which loads an entire row as soon as it is available, allows you to access the fields returned in any order until the next row is read. When using SequentialAccess however, you must access the fields returned by the DataReader in order. For example, if your query returns three columns, the third of which is a BLOB, you must return the values of the first and second fields before accessing the BLOB data in the third field. If you access the third field before the first or second fields, the first and second field values are no longer available. This is because SequentialAccess has modified the DataReader to return data in sequence and the data is not available after the DataReader has read past it.
When accessing the data in the BLOB field, use the GetBytes or GetChars typed accessors of the DataReader, which fill an array with data. You can also use GetString for character data; however. to conserve system resources you might not want to load an entire BLOB value into a single string variable. You can instead specify a specific buffer size of data to be returned, and a starting location for the first byte or character to be read from the returned data. GetBytes and GetChars will return a long
value, which represents the number of bytes or characters returned. If you pass a null array to GetBytes or GetChars, the long value returned will be the total number of bytes or characters in the BLOB. You can optionally specify an index in the array as a starting position for the data being read.
Example
The following example returns the publisher ID and logo from the pubs sample database in Microsoft SQL Server. The publisher ID (pub_id
) is a character field, and the logo is an image, which is a BLOB. Because the logo field is a bitmap, the example returns binary data using GetBytes. Notice that the publisher ID is accessed for the current row of data before the logo, because the fields must be accessed sequentially.