Re: shorts and longs



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


.



Relevant Pages

  • Re: shorts and longs
    ... aren't all TIFF IFD entries and tag values stored as unsigned values? ... so, when i Dim m_Compression as Integer and the return value is 32773, i get ... > as a long which is what you store in your application. ... > Dim TempWord As Integer ...
    (microsoft.public.vb.general.discussion)
  • DpAPI Encrypted Aes Key Problems
    ... Friend Function CreateKeyAs String ... Dim aesProvider As New RijndaelManaged ... Public Enum Store ... ' Not passing optional entropy and use the machine store ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Storing a value for later use in vba or a Macro
    ... Dim strCurrentCustomer as String ... do you programmatically store a value from one record to use in ...
    (microsoft.public.access.gettingstarted)
  • RE: .movefile
    ... You should be able to store the file name in a variable after you open the ... 'This example move all Excel files from FromPath to ToPath. ... Dim FromPath As String ...
    (microsoft.public.excel.programming)
  • Re: Module Data Storage
    ... please note that the multi-select listbox is not designed to store data. ... that person's hobbies from the list of all possible hobbies. ... Private Sub ClearHobbySelections() ... Dim rs As DAO.Recordset ...
    (microsoft.public.access.modulesdaovba)

Loading