如何添加 /删除控制字符的1个字节STX&斯威夫特
我正在编写一个代码,我想在字符串& 1个字节
etx
在Swift String的末尾。
不知道该怎么做。
示例:
<1B>---<3B>--<1B>-<1B>---<3B>--<1B>
<STX><String><ETX><STX><String><ETX>
其中1b = 1个字节&amp; 3b = 3字节
STX= Start of Text
ETX= End of Text
控制字符参考:输入链接在这里
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只使用 STX 和
ETX
没有特殊的逃生字符,您可以使用其Unicode stallar值0x02
和0x03 < /代码>。
直接在字符串中,
您可以使用字符串字面的字符串构造字符串,如果需要,则可以插值:
您可以交叉检查打印每个字节的数字值:
串联字符串,
您也可以定义一些字符串常数:
并通过串联构建字符串部分。在此示例中,
field1
和field2
是任意长度的字符串变量,这些变量被转换为完全3个字符长度:You could just use the special characters in string litterals. Considering that the ASCII control codes
STX
andETX
have no special escape character, you could use their unicode scalar values0x02
and0x03
.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,
field1
andfield2
are string variables of arbitrary length that are transformed to exactly 3 character length: