-1

I have been trying to find a replacement string function that would allow me to use std::string like behaviour in C#. I just need it for some code I ported across from C++ to C# that had std::strings in them. I've read about converting the strings to byte array and then working it out from there although I am unable to do so. Any possible suggestion of doing this with an example code? Please note the below code was written in C++ using std::strings instead of C# Unicode string.

C++ Code

std::string DeMangleCode(const std::string& argMangledCode) const
{
   std::string unencrypted;
   for (uint32_t temp = 0; temp < argMangledCode.size(); temp++)
   {
      unencrypted += argMangledCode[temp] ^ (434 + temp) % 255;
   }
   return unencrypted;
}

Mangled Input: ‚‡…ƒ

Output: 1305

Neophile
  • 5,132
  • 14
  • 48
  • 99
  • 1
    But that's not C++ code. – Dave Doknjas Mar 31 '14 at 15:34
  • Yup it isnt, its just an effort to show what I actually mean code-wise. As I mentioned, just replace the strings to std::string to get the C++ version of the code. – Neophile Mar 31 '14 at 15:35
  • I think what you are looking for is the [`Chars`](http://msdn.microsoft.com/en-us/library/system.string.chars(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1) method. – Zac Howland Mar 31 '14 at 15:36
  • My C/C++ is very rusty, but IIRC `std::string` talks `char`, and `char` is single-byte. In that case, you should be using `byte[]`, not `string` – Marc Gravell Mar 31 '14 at 15:37
  • Yup, you are right Marc. That's what I have been reading through on forums as well. Any possible answers? – Neophile Mar 31 '14 at 15:39
  • Seen this one, but I'm not sure how I can convert my code to suit this.http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array – Neophile Mar 31 '14 at 15:40
  • `std::string` does not have `^=` operator that takes a `char` (or any other argument type). I can't even guess what would that operator do. What would you like to happen in this code? Give us example input and example output. – Dialecticus Mar 31 '14 at 15:43
  • Its basically decrypting the code. Input possibly is lets say: '‚‡…ƒ' and output would be 1305. (just an example - not the exact code I have stated above). – Neophile Mar 31 '14 at 15:44
  • 1
    We need exact code, and only then would we know what is exact translation to C#. – Dialecticus Mar 31 '14 at 15:45
  • Yup just updated it. Added exact C++ code. – Neophile Mar 31 '14 at 15:46
  • Strings in C# are Unicode (UTF-16) in general, so with modulo 255 information would be lost. If it is guaranteed that no character would go above ASCII range then you could use `StringBuilder` for `unencrypted` variable, and return `unencrypted.ToString()`. – Dialecticus Mar 31 '14 at 15:49
  • -1: It is still unclear what exactly is missing from `string` for you - C# `string` is already indexable, you can add characters to existing string... (indeed @Dialecticus suggestion about `StringBuilder` is proper approach, but direct translation of above code does not require it) – Alexei Levenkov Mar 31 '14 at 15:59
  • The name of this function is "DeMangleCode". Give us one mangled input, and expected demangled output. – Dialecticus Mar 31 '14 at 16:01
  • I actually did as a comment.. Updating my question with that change. – Neophile Mar 31 '14 at 16:03
  • @TheNewbie I updated my answer. It should work for you. – Dialecticus Apr 01 '14 at 10:20

1 Answers1

2

The following code will return "1305" for input "‚‡…ƒ". The trick was to figure out which code page was used when the string was mangled. It was code page 1252.

static public string DeMangleCode(string argMangledCode)
{
    Encoding enc = Encoding.GetEncoding(1252);
    byte[] argMangledCodeBytes = enc.GetBytes(argMangledCode);
    List<byte> unencrypted = new List<byte>();
    for (int temp = 0; temp < argMangledCodeBytes.Length; temp++)
    {
        unencrypted.Add((byte)(argMangledCodeBytes[temp] ^ (434 + temp) % 255));
    }
    return enc.GetString(unencrypted.ToArray());
}
Dialecticus
  • 15,040
  • 5
  • 36
  • 88
  • Thanks a lot. I dint know about this code page although I was trying it myself just sometime back. Thanks for updating the answer. – Neophile Apr 01 '14 at 10:26