为什么人物在处理他们之后会出现像
Why characters shows up like � after dealing with them?
我正在做一种加密算法,但我有一个可怕的问题,当处理一些特殊字符,如"
每当我在内存或类似的地方进行复制时,这个问题就会出现。我写了一小段代码来解释它:
#include<string.h>
#include<iostream>
using namespace std;
int main() {
char ar[]="à";
char bf[]="a";
char cd;
cout <<"Before:n" << ar <<"t" <<bf << endl;
cd=ar[0];
ar[0]=bf[0];
bf[0]=cd;
cout <<"After:n" << ar <<"t" <<bf << endl;
}
由于我对此知之甚少,我无法解决这个问题。我怎样才能解决这个问题?
据我所知,我们无法在ascii码中找到字符à
。因此,使用wchar_t代替char。因为char只有8位,只能处理256个不同的字符。
im dealing with arrays in ma code ...and using wchar_t to store the char in cd didn't help
在这种情况下使用wchar_t数组
声明wchar_t
字符串
wchar_t wptr[] = L"Your String";
声明wchar_t
char
`wchar_t wc=L'A';
声明一个wchar_t char数组
`wchar_t aa[]={L'A',L'B'};`
所以你的代码变成了
#include<string.h>
#include<iostream>
using namespace std;
int main() {
wchar_t ar[]=L"à";
wchar_t bf[]=L"a";
wchar_t cd;
cout <<"Before:n" <<ar <<"t" <<bf <<endl;
cd=ar[0];
ar[0]=bf[0];
bf[0]=cd;
cout <<"After:n" <<ar <<"t" <<bf <<endl;
}
这就解决了问题
如果在Windows操作系统上从DOS命令提示符运行此程序,则必须将命令窗口设置为使用与ANSI兼容的活动代码页。下面的代码页在我的系统上工作:
chcp 1252
运行程序前输入chcp命令
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在类定义之后定义一个私有方法
- 在循环C++中指定字符串之后,不会打印该字符串
- C++宏忽略之后的内容
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- strncpy之后的char数组的错误行为
- 计算十进制 c++ 之后的数字
- "x += x--"之后的 x 是什么?
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- execlp() 在 fork() 之后无法正常工作
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?
- 我认为我的代码很好,但它在 cin a 之后停止并且没有进一步?
- 如何在MISRA C++之后实施CRTP
- 我怎样才能有 2 个程序并让用户选择他们想要使用的程序?
- 在 OpenCV 的 namedWindow 之前或之后初始化 Tesseract
- 为什么人物在处理他们之后会出现像