C++ 特殊字符

标签 c++ character-encoding internationalization

我遇到以下问题

#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/

相关文章:

c++ - "Reference qualifier correctness"还是应该将非常量方法应用于右值?

c++ - 具有成员函数的 G++ may_alias

java - Android webview.postUrl(url,Encodingutils.getBytes(postData ,"BASE64")) 从 postdata 字符串中删除 "+"

ios - 为不受支持的语言选择本地化

java - 我可以将 C++ 代码与 JAVA 代码一起使用吗?

c++ - 在工作组 Windows Server 2008 R2 上运行时 NetUserAdd 失败

java - BeanIo 无法读取特殊字符

mysql - 从 R 中的 MySQL 获取 UTF-8 文本返回 "????"

ruby-on-rails - Activeadmin 资源名称

vuejs3 - Nuxt3 + Cypress : "useI18n is not defined" error in component testing