Android Code Snippet [B4X] HexToColor and ColorToHex


Staff member
Licensed User
Converts hex color strings to a color int value and vice versa:
Private Sub ColorToHex(clr As Int) As String
    Dim bc As ByteConverter
    Return bc.HexFromBytes(bc.IntsToBytes(Array As Int(clr)))
End Sub

Private Sub HexToColor(Hex As String) As Int
    Dim bc As ByteConverter
    If Hex.StartsWith("#") Then 
        Hex = Hex.SubString(1)
    Else If Hex.StartsWith("0x") Then 
        Hex = Hex.SubString(2)
    End If
    Dim ints() As Int = bc.IntsFromBytes(bc.HexToBytes(Hex))
    Return ints(0)
End Sub
Depends on: B4A / B4J - ByteConverter, B4i - iRandomAccessFile