简体   繁体   中英

Need some help converting C# method to C++

I am a C# guy who is desperately trying to learn C++ and port some old code over. Been doing OK so far but the following method has me stumped. If anyone could give me some pointers (sorry for pun) I would be grateful.

C# method:

public static string crappyEncryption(String userKey)    
{    
    StringBuilder eStr = new StringBuilder();    
    String key1 = "somehorriblelongstring";    
    String key2 = "someotherhorriblelongstring";    
    for (int i = 0; i < userKey.Length; i++)   
    {    
        eStr.Append(key2[key1.IndexOf(userKey[i])]);    
    }    
    return encodeTo64(eStr.ToString());    
} 

encodeTo64 is a local method which I have solved in C++. This weird method (if you were wondering) was a small encryption method I came up with that we could use mobile cross platform for non-essential string encryption.

Thanks very much

Not gonna give you the whole code, but some pointers:

  • a StringBuilder can be substituted by a std::stringstream .
  • a String is a std::string
  • it has the method length() , find() and operator[] .
  • std::stringstream has operator << for Append .
  • ToString is std::stringstream::str() .
  • you'll want to pass userKey by reference.

All concepts you don't understand can easily be found with a google search.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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