4

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

5
  • if your weird encodeTo64 is an algorithm from Base64 family (en.wikipedia.org/wiki/Base64) then I wouldn't call it weird, it's just a way to deal with textual communications when you need to transfer binary data. Commented Oct 17, 2012 at 21:18
  • 1
    Is there something specific that has you stumped? This doesnt look too bad. The std::string type will be what you use in place of the string builder and string. std::string has an append function, and a find function to use in place of IndexOf. Commented Oct 17, 2012 at 21:21
  • miscommunication - I was referring to my crappyEncryption() method as wierd. :) Commented Oct 17, 2012 at 21:22
  • I have got the code up to the loop going by using std:: string and std::wstring but I am getting an invalid conversion exception for the line within the loop: eStr = key2[key1.find(userKey[i])]; I have eStr defines as std::string *eStr and the key strings as std::wstring Commented Oct 17, 2012 at 21:28
  • If the purpose of porting the code is simply to learn C++, then sure. Generally, as a principle, porting code is a waste of productive time, when you could be wrapping the code instead. Commented Oct 17, 2012 at 21:38

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.