简体   繁体   中英

C++ special characters

I have the following problem

#include <iostream>
#include <stdio.h>
#include <string.h>

int main (){
   string data = "\xd7\x91\xd7\x90" ; 

   data << data<<endl;
}

The output is: בא

But with an input.

#include <iostream>
#include <stdio.h>
#include <string.h>

int main (){
    char rrr[100];
    cout << "Please enter your string:" ;   
    scanf("%s",rrr);

    cout<< rrr <<endl;
}    

The input I type is: \\xd7\\x91\\xd7\\x90

The output I see on the screen is: \\xd7\\x91\\xd7\\x90

So my question is how can I convert the input \\xd7\\x91\\xd7\\x90 to בא ?

You can change your scanf statement:

int a, b, c, d;
if (scanf("\\x%x\\x%x\\x%x\\x%x", &a, &b, &c, &d) == 4)
    // a, b, & c have been read/parsed successfully...
    std::cout << (char)a << (char)b << (char)c << (char)d << '\n';

Still, it's better to learn to stream in hex values ala:

char backslash, x;
char c;

while (std::cin >> backslash >> x >> std::hex >> c && backslash == '\\' && x == 'x')
    // or "for (int i = 0; i < 4 && std::cin >> ...; ++i)" if you only want four
    std::cout << c;

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