• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1382
• Last Modified:

# Declare an unsigned long variable

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
pgwjb
Asked:
• 3
• 3
• 2
• +3
1 Solution

Commented:
I think you are looking for a LONG
0

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

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

Commented:
"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

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

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

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

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

Commented:
soory for the doubled comment
0

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

Commented:
Per recommendation, force-accepted.

Netminder
CS Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 3
• 3
• 2
• +3
Tackle projects and never again get stuck behind a technical roadblock.