0

Okay so I have 4 values(A, R, G, B) and im trying to make them into a decimal.

public function MergeABGR(A, R, G, B)
Return (A << 24) + (R << 16) + (G << 8) + B
end function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
msgbox(MergeABGR(dim a = 255, dim r = 255, dim g = 0, dim b = 255))
End Sub

The result im getting from this is: -65282.

I should be getting: 4294902015

Anyone know why this is happening??

Dim Decimal as Long = 4294902015
Dim outA as integer = SplitA(Decimal, OutA)
Dim outR as integer = SplitR(Decimal, OutR)
Dim outG as integer = SplitG(Decimal, OutG)
Dim outB as integer = SplitB(Decimal, OutB)


Public Function SplitR(ABGR, ByRef R)

    R = ABGR And &HFF
    Return R
End Function
Public Function SplitG(ABGR, ByRef G)

    G = ABGR >> 8 And &HFF
    Return G
End Function

Public Function SplitB(ABGR, ByRef B)

    B = ABGR >> 16 And &HFF
    Return B
End Function

Public Function SplitA(ABGR, ByRef A)

    A = ABGR >> 24 And &HFF
    Return A
End Function

After all this, these are the results I get outA = 255 outR = 255 outG = 0 outB = 255

TLB Aqua
  • 23
  • 4

1 Answers1

1

First of all, turn on Option Strict(see also: what's an option strict and explicit?), so you have to define a type for everything. It'll be worth it in the long run.

Now that we have to create types for everything, your function can look like this:

Public Function MergeABGR(A as Byte, R as Byte, G as Byte, B as Byte) as UInt32
    Return (Convert.ToUInt32(A) << 24) + (Convert.ToUInt32(R) << 16) + (Convert.ToUInt32(G) << 8) + B
End Function

Where we return an UInt32, and also convert the parameters to UInt32s, so that we don't overflow the maximum value of anything. 4294902015 is sufficiently large to not fit in a signed 32 bit integer, so we switch to an unsigned 32 bit integer to hold it instead.

Community
  • 1
  • 1
James Thorpe
  • 31,411
  • 5
  • 72
  • 93