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

尽管如此,最好还是学习以十六进制值进行流式传输:

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 回答