如何在C中发送ctrl + z

时间:2022-07-03 20:31:52

I'm working with Arduino.

我正在和Arduino合作。

I want to send Ctrl+z after a string in C. I tried truncating ^Z but that didn't work. So how to do that ?

我想在C中的字符串后发送Ctrl + z。我尝试截断^ Z但是这不起作用。那怎么办呢?

3 个解决方案

#1


12  

Ctrl+Z = 26 = '\032' = '\x1A'. Either of the backslash escape sequences can be written in a string literal (but be careful with the hex escape as if it is followed by a digit or A-F or a-f, that will also be counted as part of the hex escape, which is not what you want).

Ctrl + Z = 26 ='\ 032'='\ x1A'。反斜杠转义序列中的任何一个都可以用字符串文字写入(但要注意十六进制转义,就好像它后跟一个数字或AF或af,这也将被算作十六进制转义的一部分,这不是什么你要)。

However, if you are simulating terminal input on a Windows machine (so you want the character to be treated as an EOF indication), you need to think again. That isn't how it works.

但是,如果要在Windows计算机上模拟终端输入(因此您希望将该字符视为EOF指示),则需要再次考虑。这不是它的工作原理。

It may or may not do what you want with Arduino, either; in part, it depends on what you think it is going to do. It also depends on whether the input string will be treated as if it came from a terminal.

你也许可以或不可以用Arduino做你想做的事情;在某种程度上,这取决于你认为它将要做什么。它还取决于输入字符串是否会被视为来自终端。

#2


3  

I hacked this up as I needed similar

因为我需要类似的东西,我把它砍掉了

#include <stdio.h>
#define CTRL(x) (#x[0]-'a'+1)
int main (void)
{
    printf("hello");
    printf("%c", CTRL(n));
    printf("%c", CTRL(z));
}

hope it helps 8)

希望它有帮助8)

#3


1  

I assume by "truncating" you actually meant appending.

我假设通过“截断”你实际上意味着附加。

In ASCII, CTRL+z is code point 26 so you can simply append that as a character, something like:

在ASCII中,CTRL + z是代码点26,因此您可以简单地将其附加为字符,例如:

#define CTRL_Z 26
char buffer[100];
sprintf (buffer, "This is my message%c", CTRL_Z);

The sprintf method is only one of the ways of doing this but they all basically depend on you putting a single byte at the end with the value 26.

sprintf方法只是执行此操作的方法之一,但它们基本上都取决于您将最后一个字节放在值26的末尾。

#1


12  

Ctrl+Z = 26 = '\032' = '\x1A'. Either of the backslash escape sequences can be written in a string literal (but be careful with the hex escape as if it is followed by a digit or A-F or a-f, that will also be counted as part of the hex escape, which is not what you want).

Ctrl + Z = 26 ='\ 032'='\ x1A'。反斜杠转义序列中的任何一个都可以用字符串文字写入(但要注意十六进制转义,就好像它后跟一个数字或AF或af,这也将被算作十六进制转义的一部分,这不是什么你要)。

However, if you are simulating terminal input on a Windows machine (so you want the character to be treated as an EOF indication), you need to think again. That isn't how it works.

但是,如果要在Windows计算机上模拟终端输入(因此您希望将该字符视为EOF指示),则需要再次考虑。这不是它的工作原理。

It may or may not do what you want with Arduino, either; in part, it depends on what you think it is going to do. It also depends on whether the input string will be treated as if it came from a terminal.

你也许可以或不可以用Arduino做你想做的事情;在某种程度上,这取决于你认为它将要做什么。它还取决于输入字符串是否会被视为来自终端。

#2


3  

I hacked this up as I needed similar

因为我需要类似的东西,我把它砍掉了

#include <stdio.h>
#define CTRL(x) (#x[0]-'a'+1)
int main (void)
{
    printf("hello");
    printf("%c", CTRL(n));
    printf("%c", CTRL(z));
}

hope it helps 8)

希望它有帮助8)

#3


1  

I assume by "truncating" you actually meant appending.

我假设通过“截断”你实际上意味着附加。

In ASCII, CTRL+z is code point 26 so you can simply append that as a character, something like:

在ASCII中,CTRL + z是代码点26,因此您可以简单地将其附加为字符,例如:

#define CTRL_Z 26
char buffer[100];
sprintf (buffer, "This is my message%c", CTRL_Z);

The sprintf method is only one of the ways of doing this but they all basically depend on you putting a single byte at the end with the value 26.

sprintf方法只是执行此操作的方法之一,但它们基本上都取决于您将最后一个字节放在值26的末尾。