echo \x6e in shell, but called from c++, using variable

I am trying to output a hex value using a variable for the number. Here is an example of it with no var.

system("echo '\x6e'");
//output:
//n

Perfect.

But this doesn’t work:

for (int i=1; i<7; i++) {
  system(("echo '\x"+to_string(i)+"e'").c_str());
}

//Which does not even compile. 
//compiler:
//error: \x used with no following hex digits

Well if you actually let the code run there would BE hex digits. I try \\x but then it just literally prints \x1e etc.

>Solution :

The comment explains the issue. Possible solution

system(("echo '"s + static_cast<char>(i * 16 + 0xe) + "'").c_str());

Leave a Reply