In B4R could not find a builtin function for Chr to convert an AscII DEC code to a Char literal (i.e. Chr(76) = 'L').
Workout a solution using Inline C combined with a Sub, but wondering if there is a simpler way for the Chr function. Any suggestions?
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private asciiChar As String
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Chr(76) 'L
Chr(77) 'M
End Sub
' Convert ascii dec 48 (0) to 90 (Z)
' Runs inline c to update global var asciiChar
Private Sub Chr(asciiCode As Int)
RunNative("convertAscII", asciiCode)
Log(asciiCode, "=", asciiChar)
End Sub
#if c
// Convert ascii dec to b4r string using Arduino C/C++ function: char(x) - Converts a value to the char data type
// Requires global var asciichar as string
void convertAscII(B4R::Object* o) {
String buffer = String(char(o->toULong()));
B4R::PrintToMemory pm;
B4R::B4RString* s = B4R::B4RString::PrintableToString(NULL);
pm.print(buffer);
B4R::StackMemory::buffer[B4R::StackMemory::cp++] = 0;
b4r_main::_asciichar = s;
}
#End If
Many Thanks and advice in the next post by not using a sub
Removed the Sub as replaced by next post solution.
B4X:
'DO NOT USE
' Convert AscII DEC code to AscII String
' Example: <copy>
' Chr(76) returns "L"
' </copy>
Public Sub Chr(asciiCode As Int) As String
Dim bc As ByteConverter
Dim b() As Byte = Array As Byte(asciiCode)
Return bc.StringFromBytes(b)
End Sub