没有正确处理c++字符
C++ Characters not being handled properly
我已经编写了一个基本的加密程序,它从用户的加密文本中获取输入,通常以Unicode字符的形式。但是当我这样做时:
cout << "Your decrypted string is : " << decryptstring(text, key) << endl;
它给了我错误的答案,并得到完全错误的字符代码,如è
将是-118
。
但是当我这样做的时候:
cout << "Your decrypted string is : " << decryptstring("è", key) << endl;
它工作无缝!
如何解决这个问题?如果你需要看我的其他代码,只要问。由于
我从getline(cin, text);
得到文本
decryptstring功能:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string decryptstring(string text, string key)
{
//Declare variables
int i = 1;
int result;
int tmp;
int tmp2;
vector < char > arr;
//Start decryption
while(i <= text.length())
{
/*
Get letter from the string and get letter from key
and use Vigenere cipher to decrypt the data.
*/
tmp = text[i - 1];
tmp2 = key[i - 1];
cout << tmp << endl;
//Get encrypted value
result = tmp - tmp2;
//Change it into readable stuff
arr.push_back(result);
//Increment i
i++;
}
string str(arr.begin(), arr.end());
return str;
}
您需要使用宽字符作为unicode。
decryptstring
也是如此:
std::wstring decryptstring(const std::wstring& text, const std::wstring& key) {
// Do stuff
return text;
}
然后std::wcout << L"Your decrypted string is : " << decryptstring(text, key) < std::endl;
std::wcout << L"Your decrypted string is : " << decryptstring(L"è", key) << std::endl;
,
std::wstring text; // is declared before use
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存