Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading