简体   繁体   English

将字符串拆分为字符然后返回字符串

[英]Splitting string into chars and then back to string

I was doing a simple c++ chat and i wanted to encrypt the messages with a simple made code. 我正在做一个简单的c ++聊天,我想用简单的代码加密消息。 So i thought it would be like chars ax and then it would replace them for example a -11. 所以我认为这就像chars ax,然后它将取代它们,例如a -11。 So it would need to 所以它需要

  1. Split the message into chars 将邮件拆分为字符
  2. Change the chars into number 将字符更改为数字
  3. Put them back in the right order. 按正确的顺序放回去。

So does anyone knows how to do it? 那么有谁知道怎么做? Thanks :) 谢谢 :)

A simple way of doing is to run a loop and process the characters at every iteration. 一种简单的方法是运行循环并在每次迭代时处理字符。

#include <iostream>
#include <string.h>

using namespace std;    

int main() {    

int num = 3;    //your choice for encryption
int len = 30;   //length of string
char * str = new char[len];
cin>>str;

//encrypt
for (int i = 0; i < strlen(str);i++)
{
    str[i] += num;
}

cout<<str<<endl;
return 0;
}

std::string will store arbitrary values of char , and let you access individual characters quite easily. std::string将存储char任意值,并允许您非常轻松地访问单个字符。 For encryption, however, you might prefer to work with unsigned char , which is pretty easy too -- std::string is just a typedef for std::basic_string<char> , but std::basic_string<unsigned char> is pretty easy to manage. 但是,对于加密,您可能更喜欢使用unsigned char ,这也很简单 - std::string只是std::basic_string<char>的typedef,但是std::basic_string<unsigned char>非常简单管理。

It sounds like what you want is on the order of a Ceaser cipher, though it's easiest if you just "encrypt" everything, rather than just letters. 听起来你想要的是Ceaser密码的顺序,尽管如果你只是“加密”一切,而不仅仅是字母,这是最简单的。

std::basic_string<unsigned char> s;

for (int i=0; i<s.length(); i++)
    s[i] += 5;

Then to "decrypt" you'd just do the opposite: 然后“解密”你只是做相反的事情:

for (int i=0; i<s.length; i++)
    s[i] -= 5;

A string in C++ is already an array of chars, unless your chat program uses Unicode or another character set encoding. 除非聊天程序使用Unicode或其他字符集编码,否则C ++中的字符串已经是字符数组。 In that case, things become much more complicated because the string might be a byte array fresh off the network (not characters) or it might be an array of decoded wide characters (a wstring). 在这种情况下,事情变得复杂得多,因为字符串可能是网络上新鲜的字节数组(不是字符),也可能是解码宽字符数组(wstring)。

But assuming that you have either an ASCII string of 8-bit characters or a decoded wide string of 32-bit characters, steps 1 and 2 are quite easy. 但假设你有一个8位字符的ASCII字符串或一个32位字符的解码宽字符串,步骤1和2非常容易。 Step 1 is done for you. 第1步是为您完成的。 You should be able to access each character using an array index, like mystring[1] , mystring[2] . 您应该能够使用数组索引访问每个字符,如mystring[1]mystring[2] Step 2 is easy because characters are numbers. 第2步很简单,因为字符数字。 Simply do your math on the character value. 只需对字符值进行数学运算即可。 You can add, subtract or multiply: whatever you like. 您可以添加,减去或乘以:您喜欢的任何内容。 Be aware that an 8-bit char probably has a value range from -128 to +127. 请注意,8位字符的值可能介于-128到+127之间。

The order of the characters doesn't change in the string. 字符串的顺序在字符串中不会改变。 So just leave them in place. 所以只需将它们留在原地。

If your encoding method changes the length of the string, then you will need to create a new copy of the string. 如果您的编码方法更改了字符串的长度,那么您将需要创建该字符串的新副本。 The best way to do it would be while you are doing the math. 最好的方法是在你做数学的时候。 In C++, use the push_back method to add a new character to the "back" of the new string. 在C ++中,使用push_back方法将新字符添加到新字符串的“后面”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM