[英]How can I replicate compile time hex string interpretation at run time!? c++
In my code the following line gives me data that performs the task its meant for:在我的代码中,以下行为我提供了执行任务的数据:
const char *key = "\xf1`\xf8\a\\\x9cT\x82z\x18\x5\xb9\xbc\x80\xca\x15";
The problem is that it gets converted at compile time according to rules that I don't fully understand.问题是它在编译时根据我不完全理解的规则进行了转换。 How does "\x" work in a String?
“\x”在字符串中如何工作?
What I'd like to do is to get the same result but from a string exactly like that fed in at run time.我想要做的是获得相同的结果,但来自与运行时输入的字符串完全相同的字符串。 I have tried a lot of things and looked for answers but none that match closely enough for me to be able to apply.
我已经尝试了很多事情并寻找答案,但没有一个足够匹配以至于我无法申请。
I understand that \x denotes a hex number.我知道 \x 表示一个十六进制数。 But I don't know in which form that gets 'baked out' by the compiler (gcc).
但我不知道编译器 (gcc) 以哪种形式“烘烤”了它。
What does that ` translate into?那`翻译成什么?
Does the "\a" do something similar to "\x"? "\a" 是否做类似 "\x" 的事情?
This is indeed provided by the compiler, but this part is not member of the standard library.这确实是由编译器提供的,但这部分不是标准库的成员。 That means that you are left with 3 ways:
这意味着您有 3 种方法:
popen
is available) execute it from your main program and read its input.popen
可用)从您的主程序执行它并读取它的输入。 Pretty ugly isn't it... If it was not clear until here, I strongly urge you to use the third way;-)如果直到这里还不清楚,我强烈建议您使用第三种方式;-)
If you want to translate "escape" codes in strings that you get as input at run-time then you need to do it yourself, explicitly.如果您想翻译在运行时作为输入获得的字符串中的“转义”代码,那么您需要自己明确地进行。
One way is to read the input into one string.一种方法是将输入读入一个字符串。 Then copy the characters from that source string into a new destination string, one by one.
然后将源字符串中的字符一个一个地复制到新的目标字符串中。 If you see a backslash then you discard it, fetch the next character, and if it's an
x
you can use eg std::stoi
to convert the next few characters into its corresponding integer value, and append that number to the destination string (either adding it with std::to_string
, or using output string streams and the normal "output" operator <<
).如果你看到一个反斜杠然后你丢弃它,获取下一个字符,如果它是一个
x
你可以使用例如std::stoi
将接下来的几个字符转换成它对应的 integer 值,和 append 这个数字到目标字符串(或者使用std::to_string
添加它,或使用 output 字符串流和正常的“输出”运算符<<
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.