Formatting String

Need some assistance in formatting a string that I am getting from another device over OTG serial connection.

String: eGUtilities:eEeG Park ScopeeE
Hex Code:
ESC G SOH SOH Utilities: ESC E SOH STX Park Scope ESC E

I would like to display it as:
Park Scope

Remote Device will always sends a two line answer to a command.
First line will always start with: ESC G SOH SOH
First line ends with: ESC E
Second Line start with: SOH STX
Second line ends with: ESC E

Not absolutely clear - the two lines arrive as a single string?

You are receiving the Hex Code over OTG?

Edit: OK, 1 coffee later and I do get it :grin:

What exactly do you need? If you send text, why not just send it as text and then you can receive it as text on the app side too. No additional formatting. If you absolutely want to receive bytes and then convert them to text you can use my extension:

I send a command from App, remote device responds with one string, similar to the one in original request.
I would like to remove the special characters and format it into two lines as noted.

I do not have format control over remote device. I just connect to it over rs232 and send commands and it responds.

Hi Nujj

I have coded-up an example, just testing right now.

So the principle is to split the data string into hex pairs (each hex pair = 1 Ascii), then look-up each hex pair in a List to convert. It would be prettier to use a Dictionary but it would just take too long to make it :grin:

StringViaOtg.aia (8.3 KB)

Download "StringViaOtg.aia" and Import into App Inventor thus:


To test, run on your phone via the Companion.

The Blocks:

Edited: Clear Labels and cumulative Vars to allow multiple string extractions.

Hi Chris,

Thank you for you code, I did learn a lot from it. The string I'm getting is in ASCII, with the special characters that do not get rendered correctly thus I had to convert the string to Hex to know what those characters were using Realterm connected to the remote device.

What would the approach be dealing with the Ascii string.

The device sends ASCII Strings, but not ASCII code, so non-printable characters are the issue and they are common to every string?

By the way, if you reply to my Post, rather than the whole Topic, I get a notification - so if I'm free, you would get a faster response.

Hi Naji

At the moment, we are between a rock and a hard place because App Inventor cannot handle the non-printing characters (SOH, STX etc) - in fact, if you put them in a text block, next time around the Project will not load properly.

MIT Have a potential fix for this, it was reported back in November. I can't say for sure that it will be ready for the next release, or indeed when that release is due. The problem affects App Inventor offspring too, such as Kodular.

Is it possible to convert the String to Hex via Realterm and send the Hex to the App? Something like that would be the best work-around for now.

If this trick of the Web component works in this part of the ASCII table,
you should be able to generate low end strings at run time without threat to project structures
and without the need for extensions.

P.S. These blocks can be dragged directly into your Blocks Editor workspace.

CRLF.aia (5.2 KB)


Hi Chris.

Thanks for the follow up...

I noticed that when I am in emulator mode the following code works in emulator mode but the project fails to build.
If you notice the AS2 String (red box) is rendering correctly....


You recommendation worked. :smiley:
and it compiled without errors....

A brilliant solution ABG dancing

1 Like

Thanks for the test report.
These ASCII codes are hard to test without a data stream, and the web trick is very finicky, depending on the choice of codes.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.