Re: shorts and longs
- From: "Mike D Sutton" <EDais@xxxxxxxx>
- Date: Tue, 26 Jul 2005 18:11:26 +0100
> it was my understanding that a c type short was equal to a vb type
> integer, and a c type long was equal to a vb type long.
>
> according to the tif file specs, the compression tag (259) stores it's
> value (either 1, 2, 3, 4, 5, 6, or 32773) as a short type (a vb integer)
> but as you can see, one of the valid values of the compression type is
> 32773, which is out of the range of a vb integer. so, when i Dim
> m_Compression as Integer and the return value is 32773, i get an overflow
> obviously. i can change it to Dim m_Compression as Long, but will that
> cause any trouble i'm not aware of?
This is because VB's integer types are all signed which means they're
capable of storing positive or negative numbers. Unfortunately because of
this one bit (known as the sign bit) has to be reserved to indicate whether
the number is negative or positive so this halves the maximum value to 32767
(0x7FFF) with the minimum value being -32768 (0x8000.) In other languages
you will often find unsigned integer types which have the range 0 to 65535
but are unable to store negative numbers.
To circumvent this problem in VB you can read 2-bytes from the file into a
VB integer type, then perform a signed to unsigned int conversion on it and
get it's unsigned value as a long which is what you store in your
application. You'll find this same technique used all over my Info*
classes:
'***
Private Function SWordToUWord(ByVal inWord As Integer) As Long
SWordToUWord = inWord And &H7FFF& ' Signed to unsigned Word
If (inWord And &H8000) Then SWordToUWord = SWordToUWord Or &H8000&
End Function
'***
Another thing I'll quite often if the format requires frequent reading of
unsigned types is to wrap this into a callable function:
'***
Private Function ReadUWord(ByVal inFNum As Integer) As Long
Dim TempWord As Integer
Get #inFNum, , TempWord
ReadUWord = SWordToUWord(TempWord)
End Function
'***
This way you don't have to keep a dummy integer floating around, the
function just does that for you.
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@xxxxxxxx
WWW: Http://EDais.mvps.org/
.
- Follow-Ups:
- Re: shorts and longs
- From: Lance
- Re: shorts and longs
- References:
- shorts and longs
- From: Lance
- shorts and longs
- Prev by Date: Re: shorts and longs
- Next by Date: Re: Oracle Database and VB6
- Previous by thread: Re: shorts and longs
- Next by thread: Re: shorts and longs
- Index(es):
Relevant Pages
|
Loading