Convert UDINT Variable to ASCII String

Dear all!

Currently I am facing the following issue:
I receive several UDINT Values via Modbus TCP which contains Information as characters.
E.g. the UDINT for the Network Carrier from the Modem I use is 1093730304 which means ‘A1’ as an ASCII String. If I format this value in the watch-window in Automation Studio it shows me the ‘A1’ String. But how can I convert this UDINT Number so I can use it as the ‘A1’ String e.g. in the VC4 Visualisation?

Thank you and best regards

Hi and welcome,

this can be done using brsmemcpy
Simply copy the UDINT to a STRING this way.
This of course doesn’t perform any checks if the numbers are actually in the range but at least gets you somewhere.

brsmemcpy(ADR(myString), ADR(myUDINT), SIZEOF(myUDINT));

Best regards

Edit: The only way I can come up with to check the UDINT for validity is extract every single byte and check if it’s in the proper ASCII range, so >= 32 and <= 127) before copying them to the string

Thank you so much for your quick reply, I will test it asap.
How can I access every single byte from an UDINT? I think it is not like UDINT.B0 etc… :slight_smile:
Thank you

a) brsmemcpy once more to copy it over to a BYTE array and work from there
b) bit masking / shifting

I’ll post an example (hopefully) shortly but in the meantime for a quick reply that could already help

Super, it works!
Your support is very much appreciated!

Best regards,
Wolfgang

I wasn’t quick enough with editing, therefore here my example I wanted to give, although you already made it work it seems :smiley:

Example added:

	mySTRING := '';
	FOR i := 0 TO SIZEOF(myUDINT) - 1 DO
		tmpUDINT := SHR(myUDINT, (3 - i) * 8) AND 16#FF;
		IF tmpUDINT >= 32 AND tmpUDINT <= 127 THEN
			brsstrcat(ADR(mySTRING), ADR(tmpUDINT));
		END_IF
	END_FOR

This will only add valid visible characters to the string. If you need any explanation, please feel free to ask further.
It’s not giving you any error messages etc rn but it converts your example just fine. Datatypes of my variables should be obvious :wink:
this example comes without brsmemcpy but uses brsstrcat

1 Like