How to write STX (0x2) and ETX (0x3) into the string.

char a[128] = {0};
Method 1: Know where you want to write, directly replace the character value:
a[0]=0x2;
a[1]=0x3;
Method 2: Escaping and writing
snprintf(a ,sizeof(a), “\002 \003”);
or
snprintf(a,sizeof(a), “%c %c”, 0x2,0x3);

Example:
Environment: ubuntu
ctrl alt t to enter the command line interface

vi test.c
#include<stdio.h>
#include<string.h>
int main(){
    
    
    char a[128] = {
    
    0};
    char b[128] = {
    
    0};
    snprintf(a, sizeof(a),"\002 passersby! \003");//snprintf(a, sizeof(a),"%c passersby! %c", 0x2,0x3); 
    snprintf(b, sizeof(b)," a silly dog ");
    b[0]=0x2;
    b[strlen(b)-1]=0x3;
    printf("%s\n%s\n", a, b);
    return 0;
}

Compile and view results

:wq #保存test.c文件并退出vi
gcc test.c -o a
./a

Guess you like

Origin blog.csdn.net/Judege_DN/article/details/108025936