Results 1 to 7 of 7

Thread: Strange case of Overflow error

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Strange case of Overflow error

    I am converting R, G, B colors to a Long variable.

    I believe that this is the correct way to do it:

    Dim lngColor As Long

    lngColor = B * 65536 + G * 256 + R

    R, G, and B have been defined as Bytes and their value are R=189, G=197, B=197


    I get an overflow error on

    G * 256

    but I do not get an error on B * 65536

    For example:

    Dim C1 As Long
    Dim C2 As Long
    Dim C3 As Long


    C3 = B * 65536 <---------- No error
    C2 = G * 256 <---------- Overflow
    C1 = R
    Last edited by jmsrickland; Aug 28th, 2012 at 02:57 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Strange case of Overflow error

    Either declare your rgb variables as longs or do an explict conversion before doing your mulitplication. This should give you the results you are looking for.
    Code:
    Private Sub Command1_Click()
    Dim r As Byte
    Dim g As Byte
    Dim b As Byte
    
    
    Dim C1 As Long
    Dim C2 As Long
    Dim C3 As Long
    
    Dim lngColor As Long
    
        r = 189
        g = 197
        b = 197
        
        Picture1.BackColor = RGB(r, g, b)
        
        C1 = CLng(b) * 256 ^ 2
        C2 = CLng(g) * 256
        C3 = r
        
        lngColor = C1 + C2 + C3
        
        Picture2.BackColor = lngColor
    End Sub

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Strange case of Overflow error

    I understand that and that is what I am doing but why an error on G * 256 but not on B * 65536? That's what I don't understand. If I don't get an overflow on * 65536 then wouldn't it stand to reason that you certainly wouldn't get an overflow on * 256 since it is a much smaller value. By reason I should have got an error on * 65256.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: Strange case of Overflow error

    Without qualification the literal value of 256 is assumed to be an Integer; because it is less than 32768; the result of 256 * 197 is greater than 32768 and causes the overflow. To prevent the error qualify that literal as a Long as in ...

    Code:
    Private Sub Command1_Click()
    
        Dim lngColor As Long
        Dim R As Byte, G As Byte, b As Byte
        
        R = 189
        G = 197
        b = 197
        
        lngColor = b * 65536 + G * 256& + R   'the & suffix qualifies the 256 literal as a Long
        
        Debug.Print lngColor
    
    End Sub

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Strange case of Overflow error

    if you try
    Debug.Print VarType(256)

    It will return 2 which is an interger. Since you are multipling an interger and a byte and the integer has more storage it will try to implicitly fit the results of the multiplication into an interger. That will result in the overflow you are seeing.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Strange case of Overflow error

    OK, I see. So, since in B * 65536 the 65536 is assumed to be a Long and 197 * 65536 = 12910592 that value is less than a Long so it will fit with no overflow, is this correct?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Strange case of Overflow error

    You got it.

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