2

私は次の問題を抱えています

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

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

   data << data<<endl;
}

出力は次のとおりです。בא

しかし、入力があります。

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

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

    cout<< rrr <<endl;
}    

私が入力する入力は次のとおりです。\xd7\x91\xd7\x90

画面に表示される出力は次のとおりです。\xd7\x91\xd7\x90

\xd7\x91\xd7\x90だから私の質問は、入力をどのように変換できבאますか?

4

1 に答える 1

4

scanfステートメントは次のように変更できます。

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';

それでも、16進値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;
于 2012-07-03T10:56:23.583 に答える