Article ID: 171652 - View products that this article applies to.
This article was previously published under Q171652
This article demonstrates how to copy the individual bytes of data in a multi-byte data type, such as a long integer or a single-precision floating- point number, into a byte array. It also illustrates how to take an array of bytes and turn it into a specific data type that Visual Basic (and VBA) will recognize.
This code shows how to move the separate bytes of a long integer into a four-element byte array and also how to turn the byte array back into a long integer. In a Visual Basic project, place the following code in the declarations section:
A long integer consists of four bytes. These four bytes are recognized by Visual Basic to be one number. Use the following subroutine to move the individual bytes into a byte array:
The first parameter passed to CopyMemory is the first element of the array into which the bytes will be stored. (NOTE: The entire array is passed to longToBytes.) The second parameter is the source variable. The third parameter is how many bytes will be moved. Because a long integer consists of four bytes, this parameter has been set to 4.
To go the other way, turning a byte array back into a long integer variable, use the following code:
NOTE: As with any API call, passing improper memory addresses can cause data corruption or could cause your program to terminate abnormally.
For more information on the use of the RtlMoveMemory function in Visual Basic, please see the following article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/129947/EN-US/ )Win32 Replacement for the hmemcpy Function
Article ID: 171652 - Last Review: February 12, 2007 - Revision: 4.5