I am writing a code, where I want to add 1 byte of STX at the start of the string & 1 byte of ETX at the end of the swift string.
Not sure how to do it.
Example:
<1B>---<3B>--<1B>-<1B>---<3B>--<1B>
<STX><String><ETX><STX><String><ETX>
Where 1B = 1 Byte & 3B = 3 Byte
STX= Start of Text
ETX= End of Text
Control Characters Reference: enter link description here
You could just use the special characters in string litterals. Considering that the ASCII control codes
STXandETXhave no special escape character, you could use their unicode scalar values0x02and0x03.Directly in the string literal
You can construct the string using a string literal, if needed with interpolation:
You can cross-check printing the numeric value of each byte :
Concatenating the string
You can also define some string constants:
And build the string by concatenating parts. In this example,
field1andfield2are string variables of arbitrary length that are transformed to exactly 3 character length: