|
-
Aug 28th, 2012, 02:53 PM
#1
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.
-
Aug 28th, 2012, 03:12 PM
#2
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
-
Aug 28th, 2012, 03:29 PM
#3
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.
-
Aug 28th, 2012, 04:14 PM
#4
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
-
Aug 28th, 2012, 04:20 PM
#5
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.
-
Aug 28th, 2012, 04:28 PM
#6
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.
-
Aug 28th, 2012, 04:33 PM
#7
Re: Strange case of Overflow error
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|