RoiHatam
RoiHatam

Reputation: 896

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 בא?

Upvotes: 2

Views: 4778

Answers (1)

Tony Delroy
Tony Delroy

Reputation: 106068

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;

Upvotes: 4

Related Questions