B4R Question How to convert string to b4rstring?

santook

Member
I want to convert string to b4rstring, for example, to use the JSON Library in inline C. But the following code seems to be wrong. MCU is constantly reset.

String to B4RString:
Sub Process_Globals
    Public Serial1 As Serial
    Type TestC(A1 As String,A2 As Byte)
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Dim x As TestC
    RunNative("test",x)
    Log(x.a1,",",x.a2)
End Sub



#if C
    String s;

    void test(B4R::Object* o)
    {
        _testc *tmp;
        tmp=o->data.PointerField;
        s=String("Hello World!");
        tmp->A1->data=s.c_str();
        tmp->A2=33;
    }
#End If
 

thetahsk

Active Member
Licensed User
Longtime User
I want to convert string to b4rstring, for example, to use the JSON Library in inline C. But the following code seems to be wrong. MCU is constantly reset.
...

#End If[/CODE]
Here is a conversion sample with GlobalStore.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    GlobalStore.Put(0, "This is the result slot")
    RunNative("get_string","from B4R")
    Log(GlobalStore.Slot0)
End Sub

#if c

void get_string(B4R::Object* msg) {
   const char* str = (const char*)msg->data.PointerField;
   String string="from inline c..";
   string+=str;
   B4R::B4RString str_B4R;
   b4r_main::_globalstore->_put(0,str_B4R.wrap(string.c_str())->GetBytes());
   }

#End If
 
Upvote 0
Top