1

I get the error:'Input string was not in a correct format'

This code, when I use it in C#, works fine, but I translated it myself, so there could be an error there. (I normally use this for text encryption, as it is short and quick)

This is my C++ code:

void encrypt()
{
    string psw = "mystring";
    System::String^ encr;
    int tot;
    int num;
    int lng = psw.size();
    char pswchar[1024];
    strcpy_s(pswchar, psw.c_str());
    System::String^ istr;
    for (int i = 0; i < lng; i++)
    {
        {
            ostringstream ss;
            ss << pswchar[i];
            istr = gcnew System::String(ss.str().c_str());
        }
        num = int::Parse(istr) + 15; // << I get the error here
        tot += num;
    }
    ostringstream convert;
    convert << tot;
    encr = gcnew  System::String(convert.str().c_str());
    File::WriteAllText("C:\myfolder\mypath.txt", encr);
}

This is my C# Code:

void encrypt()
{
    string psw = "mystring";
    string encr;
    char[] pswchar = psw.ToCharArray();
    for (int i = 0; i < pswchar.Length; i++)
    {
        int num = Convert.ToInt32(pswchar[i]) + 15;
        string cvrt = Convert.ToChar(num).ToString();
        encr += cvrt;
    }
}
6
  • int::Parse is suspicious. You should not convert C# to C++ but think and code in C++11! C# and C++ are different languages (like English and German are). Commented Oct 1, 2013 at 13:55
  • 2
    did you try debugging? Commented Oct 1, 2013 at 13:56
  • I debugged it, and it built successfully, but when I ran the void, I got the error. Commented Oct 1, 2013 at 13:58
  • "Encryption" might be a bit inaccurate as a description of what this code does. Just saying. Commented Oct 1, 2013 at 13:58
  • 1
    @Joe: Don't edit your question to include the answer. The question should remain a question, so that the question and answers will be useful to other people in the future. Commented Oct 1, 2013 at 16:03

1 Answer 1

2

I think this is what you asked for... but it is a bit crazy:

#include <string>

using namespace System;

std::string encrypt(const std::string& s) {
  std::string r(s);
  for (int i = 0; i < r.size() ; ++i) {
    r[i] += 15; // !!!
  }
  return r;
}

int main(array<System::String ^> ^args)
{
    std::string s = "Hello World";
    System::String^ ss = gcnew String(encrypt(s).c_str());
    Console::WriteLine(ss);
    return 0;
}

Output:

Wt{{~/f~?{s
Sign up to request clarification or add additional context in comments.

1 Comment

@Joe Your algorithm only works if the character set and encoding (aka code page) used by the thread is one where offsetting every possible byte by 15 modulo 256 always results in valid encoded characters. That would be true of CP437 and a few other common ones but not Windows-1252 (it has holes in it). System::String(char*) will substitute ? for invalid code units, which is exactly what happens to the r in the answer's test string. Read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets-No Excuses!.

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.