C#中std :: string的替代方案

时间:2014-03-31 15:29:59

标签: c# c++ string type-conversion

我一直在尝试找到一个替换字符串函数,它允许我在C#中使用类似std :: string的行为。我只需要将它从C ++移植到C#中的一些代码,其中包含std :: strings。我已经读过将字符串转换为字节数组然后从那里开始工作,尽管我无法这样做。用示例代码做任何可能的建议吗?请注意,下面的代码是用C ++编写的,使用std :: strings而不是C#Unicode string。

C ++代码

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:,‡...ƒ

输出:1305

1 个答案:

答案 0 :(得分:2)

以下代码将返回“1305”以输入“,‡...ƒ”。诀窍是找出字符串被破坏时使用的代码页。这是代码页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());
}