Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print string without escaping characters

Tags:

c++

This code:

void Controller::write(const std::string& str) {
    std::cout << "Writing: [" << str << "] to board." << std::endl; 
    s.write(str);

takes a string and pushes it over a serial link to a microcontroller. (This works fine). However, when I try to push a like this: write("ats203?\r"), the console output looks like this:

] to board.ts203?

So the Writing: [a gets overwritten by the ] to board. after std::cout encounters the \r in the string.

How can I print the string as a bunch of characters rather than mangling console output when an escape character is encountered?

EDIT: To clarify.

I want my string to be ats203?\r. This is because the microcontroller is controlled using a slightly modified version of an AT command, and doesn't accept the command until it reads a \r. When the string is pushed to the microcontroller, it reads the \r as a carriage return, and acts on things it read prior to the \r.

What I want is to display the string that I've constructed. There is a bunch of other commands elsewhere that get created depending on flags that are set at runtime. So I end up with strings that look something like ate1\ratv0\rat+OSA=7\rat+OSX=255\r.

I would like to see what I've constructed and what I'm pushing. I can't do this, because std::cout reads the carriage return, acts on it, and mangles my console output.

EDIT: Clarification, again: I want std::cout << "Writing: [" << str << "] to board." << std::endl; to produce (when str is ats203?\r):

Writing: [ats203?\r] to board. 
like image 347
simont Avatar asked Feb 24 '12 17:02

simont


People also ask

How do you not print an escape character in Python?

Adding “r” or “R” to the target string triggers a repr() to the string internally and stops from the resolution of escape characters.

How do you ignore an escape character?

To ignore all the escape sequences in the string, we have to make a string as a raw string using 'r' before the string. After that escape sequence will also be considered normal characters.

How do I print a character in escape string?

, \t, \r, etc., What if we want to print a string which contains these escape characters? We have to print the string using repr() inbuilt function. It prints the string precisely what we give.

How do you print a string with special characters in Python?

How do you show special characters in Python? Use repr() to print special characters Call repr(string) to return string with special characters escaped.


3 Answers

void Controller::write(const std::string& command_string) {

    std::string display_string = std::regex_replace(command_string,std::regex("(\r)"),"\\r");

    std::cout << "Writing: [" << display_string << "] to board.\n"; 
    s.write(command_string);

So then write("ats203?\r") will literally print out

ats203?\r
like image 59
bames53 Avatar answered Oct 17 '22 18:10

bames53


Here's a quick loop to generate a readable string.

std::string str2;
for (int i = 0;  i < str.length();  ++i)
{
    if (str[i] == '\r')
    {
        str2.push_back('\\');
        str2.push_back('r');
    }
    else
        str2.push_back(str[i]);
}
like image 23
Mark Ransom Avatar answered Oct 17 '22 19:10

Mark Ransom


It isn't exactly 'mangling' the characters because of an escape sequence. The '\r' resets your cursor i.e. it is a carriage-return. Hence, your cursor goes to the beginning and whatever is typed now overwrites whatever is there on the line.

You can just erase any instance of \r in your string:

void Controller::write(const std::string& str) {
    std::string str2(str); //Make a copy of the string as you are getting a const reference
    char remove[] = "\r"; //Add any characters you wish to remove

    for (unsigned int i = 0; i < sizeof(chars); ++i) {
        str2.erase(std::remove(str2.begin(), str2.end(), chars[i]), str2.end());
    }

    std::cout << "Writing: [" << str2 << "] to board." << std::endl; 
    s.write(str);
}

NOTE: remove is a function in <algorithm>, so don't forget to #include <algorithm>.

EDIT: Code to make a copy added, thanks to jrok for pointing that one out :)

like image 26
Rohan Prabhu Avatar answered Oct 17 '22 20:10

Rohan Prabhu