Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Declare an unsigned long variable

Posted on 2001-08-15
11
Medium Priority
?
1,075 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 200 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

927 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