我遇到以下问题
#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
转换为 בא
?
最佳答案
您可以更改 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;
关于C++ 特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11309320/