ACC2000: How to Convert Unsigned Integers to Long Integers

ID: Q210571


The information in this article applies to:
  • Microsoft Access 2000

Advanced: Requires expert coding, interoperability, and multiuser skills.


SUMMARY

There are situations in which calling functions from external dynamic-link libraries (dlls) returns a 2-byte unsigned integer. Visual Basic for Applications does not support this data type. So that Access can correctly evaluate this data type, you need to convert it from an unsigned integer to a Long Integer data type.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
The Integer data type has a range of -32,768 to 32,767. An unsigned integer has a range of 0 to 65,536.

Access uses the most significant bit to set the sign of the value. Therefore, when a value exceeds 32,767, bit 16 is set to reflect a negative number. To evaluate an unsigned integer, you must manually adjust bit 16.

There are two methods to convert to and from the unsigned integer type to the Long Integer data type. The first method uses basic arithmetic (65,536 is subtracted or added to the unsigned integer). The second uses bit-wise operators. The arithmetic method works as well as the bit-wise method; however, the arithmetic method may be more readable, while the bit-wise method may be faster when executed repetitively.

Open a new module or a previously created module and enter the following code:

'*************************************************************
'Declarations section of the module.
'*************************************************************

Option Explicit

'=============================================================
' Arithmetic Method:
' ------------------
' Create the following ar_ConvertFromUnsignedInteger& (Uint%)
'    and ar_ConvertToUnsignedInteger% (Bytes&) function in the
'    Module. This first function reads in an unsigned integer
'    and returns the converted value as a long. The second
'    function reads in a long and returns an unsigned integer.
'=============================================================

Function ar_ConvertFromUnsignedInteger& (Uint%)
   If Uint% < 0 Then
      ar_ConvertFromUnsignedInteger& = Uint% + 65536
   Else
      ar_ConvertFromUnsignedInteger% = Uint%
   End If
End Function

Function ar_ConvertToUnsignedInteger% (Bytes&)
   If Bytes& > 32767 Then
      ar_ConvertToUnsignedInteger% = Bytes& - 65536
   Else
      ar_ConvertToUnsignedInteger% = Bytes&
   End If
End Function

'=============================================================
' Bitwise Method:
' ---------------
' Create the following bw_ConvertFromUnsignedInteger& (Uint%)
'    and bw_ConvertToUnsignedInteger% (Bytes&) function in the
'    Module. This first function reads in an unsigned integer
'    and returns the converted value as a long. The second
'    function reads in a long and returns an unsigned integer.
'    The message box statement in the second function is used
'    to prevent an overflow message when the value passed to
'    the function is greater than 64 kilobytes.
' To illustrate what is taking place in the first bitwise function:
'    Uint% equals -23584, a value returned from an external dynamic
'    link library that is an unsigned integer and must be
'    converted to an long:
'        1010001111100000 (-23584)
'    AND   11111111111111 (7FFF)
'        ----------------
'          10001111100000 (41952)
'=============================================================

Function bw_ConvertToUnsignedInteger% (Bytes&)
   Dim x%
   If Bytes& > 65535 Then
      MsgBox "You passed a value larger than 65535"
      Exit Function
   End If

   x% = Bytes& And &H7FFF
   bw_ConvertToUnsignedInteger% = x% Or -(Bytes& And &H8000)
End Function

Function bw_ConvertFromUnsignedInteger& (Uint%)
   bw_ConvertFromUnsignedInteger& = Uint% And &HFFFF&

'-------------------------------------------------------------
' The &HFFFF& requires the & at the end of the hex number. This
'    qualifies the hex number as 32-bit versus 16-bit.
'-------------------------------------------------------------

End Function 

Examples of Using These Functions

An external dynamic-link library requires and returns an unsigned integer. The Declare statement looks like the following:

Declare Function External_Call% Lib "your.dll" (ByVal ValueToPass%)  
The Declare statement uses Integer data types because Access does not support unsigned integers. However, the external dynamic-link library returns an unsigned integer that must be converted; therefore, the code may appear as follows:

x% = External_Call(bw_ConvertToUnsignedInteger%(41952))
y& = bw_ConvertFromUnsignedInteger&(x%) 


REFERENCES

For more information about converting code from earlier Microsoft Access versions, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type "Convert Code that calls a DLL" in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Additional query words:

Keywords : kbprg kbdta
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 7, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.