aidan aidan - 8 days ago 5x
C++ Question

C++ Characters not being handled properly

I have a basic encryption program that I have written, it takes input from the user from their encrypted text, usually in the form of Unicode characters. But when I do this:

cout << "Your decrypted string is : " << decryptstring(text, key) << endl;

It gives me the wrong answer and gets the character codes wrong completely such as
will be

However when i do:

cout << "Your decrypted string is : " << decryptstring("è", key) << endl;

It works seamlessly!

How do I fix this? If you need to see my other code just ask. Thanks

I get text from
getline(cin, text);

decryptstring function:

#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

//Increment i

string str(arr.begin(), arr.end());

return str;


You need to use wide characters for unicode.

So have decryptstring thus:

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