Results 1 to 3 of 3

Thread: Code for working with Unsigned Shorts

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    Code for working with Unsigned Shorts

    In VB6, the Integer data type is equivalent to a Short (signed short) in C. Now when you encounter a UShort (unsigned short) in a file or in a structure returned from a DLL call, what do you do? You can either hope that the value stored in it happens to be less than 32768 (a region in which Shorts and UShorts are identical), or try to find a way to get the full range of possible UShort values represented in VB6. My code here does the latter.

    Code:
    Private Function UShortToInt(ByVal Value As Integer) As Long
        UShortToInt = Value And &HFFFF&
    End Function
    
    Private Function IntToUShort(ByVal Value As Long) As Integer
        IntToUShort = (Value And &H7FFF) - (Value And &H8000)
    End Function

    When you get a UShort value, you simply use UShortToInt to convert it to Int (what's called Long in VB6), which even though it is technically a signed data type it can represent all positive values that UShort can. This gives you access to the full range of values that you were intended to be able to have access to in the UShort field from whatever file you read the data from. If you need to save a UShort to file, just work with the data in an Int and then use IntToUShort to convert it to a UShort prior to saving it to the file.

  2. #2
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    Re: Code for working with Unsigned Shorts

    It's too bad you can't directly do math on these things, as well as unsigned Long and unsigned LongLong. When in VB6, you've always got to cope with the sign bit.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Code for working with Unsigned Shorts

    One of my favorite sites for things like this: VBSpeed
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width