Android Question Passing hex value to Sub

hakha4

Member
Licensed User
Longtime User
I wan't to create a layout from code and set a number of properties, color for a button as one. Data is fetched from a SqLite table and buttons in this case is created ok if 'btncolor' is excluded. I can't figure out how to pass the HEX value for color ( like 0xFF7FFFD4) to sub below. (0xFF7FFFD4 is saved as BLOB in sqlite, is that correct??)

(NumberFormatException) java.lang.NumberFormatException: For input string: "0xFF7FFFD4"



B4X:
Public Sub Initialize(act As Activity, left As Int, top As Int, width As Int, height As Int, name As String, label As String,btncolor As ???????????)
    mName = name
    mButton.Initialize("button")

    mButton.Text = label
    mButton.Color=btncolor
    act.AddView(mButton, left, top, width, height)
    
  
End Sub
Can someone point me to a solution
 

emexes

Expert
Licensed User
Color is a 32-bit Int, so just save it as such in your SQL table.

upload_2019-3-16_19-59-49.png


Conceptually, behind the scenes, those 32 bits are partitioned into four individual 8-bit numbers (ie bytes), which each has a range of 0..255 (decimal) = 00..FF (hex) = 00000000..11111111 (binary). Three of these numbers are the red, green and blue intensity levels (where 0 = off ie black, 255 = full intensity, 128 = half-intensity), and the fourth number is often the alpha value (where 0 = transparent, 255 = opaque/solid, 128 = 50% translucent).

These numbers are often DISPLAYED in hexadecimal rather than decimal, eg as FF7FFFD4 rather than 4286578644, because that makes it easier to see the 8-bit numbers within, eg, I can see that the alpha value is FF (hex) = solid/non-transparent.

The "0x" at the front is the C/Java way of saying: this is a hexadecimal number. Most BASICs use "&H" to indicate hexadecimal numbers, but because B4A/J has both Java and BASIC heritage, and presumably because it was easier to transliterate, B4A/J uses the "0x" way. Personally, I would have preferred that compatibility decisions defaulted to minimizing the differences for people migrating from VB etc, but on the bright side, look upon it as broadening your linguistic arena ;-)

I have somebody in the background going on about latest Brexit yo-ha, so I've had some trouble concentrating on writing clearly here. In summary: "As Int"
 
Upvote 0

emexes

Expert
Licensed User
Parsing <> passing

although if it turned out that Alfred *does* actually have a hex color value as a *String* then this should do the job:
B4X:
Sub HexStringToInt(H As String) As Int
   
    Dim bc As ByteConverter
   
    Return bc.IntsFromBytes(bc.HexToBytes(H))(0)
   
    'slow-motion replay for those less bold ;-)
    'Dim B() As Byte = bc.HexToBytes(H)
    'Dim I() As Int = bc.IntsFromBytes(B)
    'Return I(0)
   
End Sub
and then call with:
B4X:
Dim ColorAsInt As Int = 0xFF7FFFD4
Dim ColorAsString As String = 0xFF7FFFD4    'B4A/Java will convert numeric constant to decimal string
Dim ColorAsHexString As String = "FF7FFFD4"    'note hex prefix "0x" dropped
'or DimColorAsHexString as string "0xFF7FFFD4".Replace("0x","")    'or use a safety-net
   
Log(0xFF7FFFD4)    'log numeric constant
Log(ColorAsInt)    'log Int variable
Log(ColorAsString & " = looks like number but is actually string")    'log decimal string
Log(ColorAsHexString & " = close, not quite what we're looking for")
Log(HexStringToInt(ColorAsHexString) & " = that's more like it")    'convert hex string to int
which should result in this log:
B4X:
-8388652
-8388652
-8388652 = looks like number but is actually string
FF7FFFD4 = close, not quite what we're looking for
-8388652 = that's more like it
 
Upvote 0
Top