Solved

Declare an unsigned long variable

Posted on 2001-08-15
11
645 Views
Last Modified: 2008-01-09
I have a requirement in VB to declare a unsigned long variable.  Is there an equivalant to that in VB.

Any help greatly appreciated.

Thanks
0
Comment
Question by:pgwjb
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6390198
I think you are looking for a LONG
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6390201
The following table shows the supported data types, including storage sizes and ranges.

Data type      Storage size     Range
Byte     1 byte     0 to 255
Boolean     2 bytes     True or False
Integer      2 bytes     -32,768 to 32,767
Long
(long integer)     4 bytes     -2,147,483,648 to 2,147,483,647
Single
(single-precision floating-point)     4 bytes     -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values
Double
(double-precision floating-point)     8 bytes     -1.79769313486232E308 to
-4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values
Currency
(scaled integer)     8 bytes     -922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal     14 bytes     +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;
+/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is
+/-0.0000000000000000000000000001
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 50 total points
ID: 6390285
There is no equivalent to unsigned long.  32 bit integers in VB come in one flavour, signed.  To fake an unsigned long, you need a variable that can hold more than 2 GB of combinations.  The best bet for oversized integers is Currency type, because it uses integer math (fixed point) instead of floating point it is faster.

When importing an unsigned integer from an outside source (C++ data file or something) it is best to read it in bytes and do the math to pack it into the larger format.

This is also convenient for solving endian issues if the data originates from a different platform.
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6390324
"PaulHews - AKA Math Master"

Nicely put Paul!

I think you had better listen to him!  He baffled me with all of that!   Good thing it's head for the homestead time  :)

Later Partners!
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6390589
Well maybe I can make it more concrete.

Here is an example from my Port 37 time service program.  When you access port 37 on a time server, it returns as an unsigned 32 bit integer the number of seconds that have elapsed since 00:00 (midnight) 1 January 1900 GMT.

Winsock1.GetData b(), vbByte, 4
'convert to unsigned long.  Use double to prevent overflows.
dblTime = b(0) * 256 ^ 3 + b(1) * 256 ^ 2 + b(2) * 256 ^ 1 + b(3) * 256 ^ 0

Note that you can reverse the order of the bytes if you need a different endian system. (Intel uses least significant byte first, this example uses most significant byte first.)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6390808
I agree with Paul, but one thing you need to watch out for is that (as Paul pointed out) a Currency data type is in fact an integer.  12.3456 is in fact maintained internally as 123456 and is automatically divided by 10,000 when used.  Hence although it is vastly superior to floating point data types (single and double, not to mention variants) it is somewhat slower than integers and longs.

Anthony
0
 
LVL 6

Expert Comment

by:VK
ID: 6391703
Hello, here is a comment from VB-Help:

Bring Your Hatchet

Dealing with pointers in Basic is kind of like getting in a hatchet fight without a hatchet. If you?re going to hack into places where you?re not supposed to go, you?d better arm yourself appropriately. The weapons of the well-equipped Basic pointer hacker are these: CopyMemory, VarPtr, StrPtr, and ObjPtr.


Of these, CopyMemory is the weapon of choice when you want to chop through Basic type limitations. The sidebar on the following page tells the bizarre history of CopyMemory, which isn?t really named CopyMemory. Here, we?re more interested in what it can do. For example, if you?ve ever tried manipulating bit fields in Visual Basic, you know that Basic?s lack of unsigned types and strong typing can make simple operations difficult. Here?s how you have to write a simple function that extracts the low Integer of a Long:

Function LoWord(ByVal dw As Long) As Integer    If dw And &H8000& Then        LoWord = dw Or &HFFFF0000    Else        LoWord = dw And &HFFFF&    End IfEnd Function

It?s not particularly readable, but as we?ll see in Chapter 5, it is efficient. We can make this code simpler with CopyMemory. Ignore Basic?s picky requirements and simply blast the bits you want to wherever you want them:

Function LoWord(ByVal dw As Long) As Integer    CopyMemory LoWord, dw, 2End Function

To see exactly how this works, let?s pretend for a moment that CopyMemory is written in a dialect of Basic that doesn?t yet exist. It looks something like this:

Sub CopyMemory(anyDestination As Any, anySource As Any, _               ByVal c As Long)

Of course there?s not really an Any type in Basic, and most of the time you wouldn?t want a feature so dangerous?although it?s nice to have it as an out in API functions. The first parameter of CopyMemory is a ByRef parameter (the address) of a variable of any type that you?ll write some bytes to. The second parameter is the ByRef variable you?ll copy from. The third parameter is the number of bytes to copy. If your finger slips and you type 20 instead of 2 in your CopyMemory call, nobody will complain until crash time.


What good is a LoWord function without a HiWord function? But this one gets a little more complicated.


0
 
LVL 6

Expert Comment

by:VK
ID: 6391718
Hello, here is a comment from VB-Help:

Bring Your Hatchet

Dealing with pointers in Basic is kind of like getting in a hatchet fight without a hatchet. If you?re going to hack into places where you?re not supposed to go, you?d better arm yourself appropriately. The weapons of the well-equipped Basic pointer hacker are these: CopyMemory, VarPtr, StrPtr, and ObjPtr.


Of these, CopyMemory is the weapon of choice when you want to chop through Basic type limitations. The sidebar on the following page tells the bizarre history of CopyMemory, which isn?t really named CopyMemory. Here, we?re more interested in what it can do. For example, if you?ve ever tried manipulating bit fields in Visual Basic, you know that Basic?s lack of unsigned types and strong typing can make simple operations difficult. Here?s how you have to write a simple function that extracts the low Integer of a Long:

Function LoWord(ByVal dw As Long) As Integer    If dw And &H8000& Then        LoWord = dw Or &HFFFF0000    Else        LoWord = dw And &HFFFF&    End IfEnd Function

It?s not particularly readable, but as we?ll see in Chapter 5, it is efficient. We can make this code simpler with CopyMemory. Ignore Basic?s picky requirements and simply blast the bits you want to wherever you want them:

Function LoWord(ByVal dw As Long) As Integer    CopyMemory LoWord, dw, 2End Function

To see exactly how this works, let?s pretend for a moment that CopyMemory is written in a dialect of Basic that doesn?t yet exist. It looks something like this:

Sub CopyMemory(anyDestination As Any, anySource As Any, _               ByVal c As Long)

Of course there?s not really an Any type in Basic, and most of the time you wouldn?t want a feature so dangerous?although it?s nice to have it as an out in API functions. The first parameter of CopyMemory is a ByRef parameter (the address) of a variable of any type that you?ll write some bytes to. The second parameter is the ByRef variable you?ll copy from. The third parameter is the number of bytes to copy. If your finger slips and you type 20 instead of 2 in your CopyMemory call, nobody will complain until crash time.


What good is a LoWord function without a HiWord function? But this one gets a little more complicated.


0
 
LVL 6

Expert Comment

by:VK
ID: 6391720
soory for the doubled comment
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7199975
Hi pgwjb,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept PaulHews's comment(s) as an answer.

pgwjb, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 5

Expert Comment

by:Netminder
ID: 7213382
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now