Android Question how to convert "0xf196" to chr(0xf196)

ykucuk

Well-Known Member
Licensed User
Longtime User
Hi,
I'm using a custom font and read icons from json file. How can I convert string?

B4X:
 "buttons": [
          {
            "id": "1001",
            "label": "Dashboard",
            "icon": "0xf196",
            "type": "dashboard"
          },


B4X:
cs.Typeface(FontIconName ).Size(FontSize). Color(Color).Append(icon).popall.
 

ykucuk

Well-Known Member
Licensed User
Longtime User
Use string to hex
Thread '[B4X] Hex string to number' https://www.b4x.com/android/forum/threads/b4x-hex-string-to-number.122739/

After : chr(IntValue)
Thank you for the help, but I already searched and tried.

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Error occurred on line: 287 (Helper)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    at java.lang.reflect.Array.get(Array.java:196)
    at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
    at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:584)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:383)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at b4a.example.main.afterFirstLayout(main.java:105)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:247)
    at android.app.ActivityThread.main(ActivityThread.java:8676)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
** Activity (main) Resume **
 
Upvote 0

emexes

Expert
Licensed User
I've probably gone overboard, but try adding this Sub:

B4X:
Sub CharNumberStringToChar(N As String) As Char
    
    Dim CleanString As String = N.Trim.ToUpperCase
    
    Dim CharNumber As Int = Asc("?")    'default for invalid input
    
    If CleanString.Length > 2 Then
        If CleanString.StartsWith("0X") Then    'note letter has been uppercased
            CharNumber = Bit.ParseInt(CleanString.SubString(2), 16)    'assume is valid hex
        else If CleanString.StartsWith("0D") Then
            CharNumber = Bit.ParseInt(CleanString.SubString(2), 10)    'assume is valid decimal
        else If CleanString.StartsWith("0B") Then
            CharNumber = Bit.ParseInt(CleanString.SubString(2), 2)    'assume is valid binary
        else if IsNumber(CleanString) Then
            CharNumber = CleanString    'B4J will convert from decimal string
        End If
    else if CleanString.Length > 0 Then
        If IsNumber(CleanString) Then
            CharNumber = CleanString
        End If
    End If

    Return Chr(CharNumber)
        
End Sub

and test it with:

B4X:
Log(CharNumberStringToChar("66"))
Log(CharNumberStringToChar("0b00110100"))
Log(CharNumberStringToChar("0x58"))
Log(CharNumberStringToChar("0x1F96"))

and with luck you'll get something like:

1679129975935.png
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Another shining example of brevity :D
B4X:
lb1.Text = Chr(Bit.ParseInt(st1.Replace("0x", ""), 16))
 
Upvote 0
Top