简体   繁体   中英

C++ escape sequence for binary value

Is there a way to add character to a string using a raw binary value? I know I can do something like that:

std::string output3 = std::string("\x01\x00\x01...", ...);

There it's done by character's hex value. Is is possible to specify the character by its bin value? Something like this:

std::string output1 = std::string("\b11100101\b01000000", 7);

Note: I know \\b has its meaning, it was just an example.

Short answer - C++ does not provide a means for escaping characters using binary values.

Likely explanation: it's never been considered useful enough for any compiler to implement as an extension (AFAIK), and certainly never useful enough to propose for standardisation.

If it's something you really need, I recommend you write (or modify) a preprocessor to do that for you (but you shouldn't use \\b as introducer, as that already represents the backspace character).

You can use append() to add individual characters, eg:

    std::string s = "abc";
    s.append(1, 'd');
    s.append(1, 0x65);  // 0x65 == 'e'
    std::cout << s << std::endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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