简体   繁体   English

如何在运行时复制编译时十六进制字符串解释? c++

[英]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 种方法:

  • dynamically write a C++ source file containing the string, and writing it on its standard output. Compile it and (providing popen is available) execute it from your main program and read its input.动态编写一个包含该字符串的 C++ 源文件,并将其写入其标准 output。编译它并(提供popen可用)从您的主程序执行它并读取它的输入。 Pretty ugly isn't it...是不是很丑。。。
  • use the source of an existing compiler, or directly its internal libraries.使用现有编译器的源代码,或直接使用其内部库。 Clang is probably a good starting point because it has been designed to be modular. Clang 可能是一个很好的起点,因为它被设计成模块化的。 But it could require a good amount of work to find where that damned specific point is coded and how to use that...但是可能需要大量的工作才能找到该死的特定点的编码位置以及如何使用它......
  • just mimic what the compiler does, and write your own parser by hand.只需模仿编译器的功能,然后手动编写自己的解析器即可。 It is not that hard, and will learn you why tests are useful...这并不难,并且会告诉你为什么测试是有用的......

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM