【问题标题】:JS decryption method translate to C#JS解密方法翻译成C#
【发布时间】:2021-10-15 12:15:45
【问题描述】:

我需要一些关于将 JS 代码翻译成 C# 的帮助。 JS代码:

function Decription(string) {
            var newString = '',
                char, codeStr, firstCharCode, lastCharCode;
            var ft = escape(string);
            string = decodeURIComponent(ft);
            for (var i = 0; i < string.length; i++) {
                char = string.charCodeAt(i);
                if (char > 132) {
                    codeStr = char.toString(10);
                    firstCharCode = parseInt(codeStr.substring(0, codeStr.length - 2), 10);
                    lastCharCode = parseInt(codeStr.substring(codeStr.length - 2, codeStr.length), 10) + 31;
                    newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);
                } else {
                    newString += string.charAt(i);
                }
            }
            return newString;
        }

我绑在 C# 上翻译:

private string Decription(string encriptedText)
        {
            string ft = Regex.Escape(encriptedText);
            string text = HttpUtility.UrlDecode(ft);
            string newString = "";

            for (int i = 0; i < text.Length; i++)
            {
                var ch = (int)text[i];
                if(ch > 132)
                {
                    var codeStr = Convert.ToString(ch, 10);
                    var firstCharCode = Convert.ToInt32(codeStr.Substring(0, codeStr.Length - 2), 10);
                    var lastCharCode = Convert.ToInt32(codeStr.Substring(codeStr.Length - 2, codeStr.Length), 10) + 31;
                }
            }

        }

但是我如何翻译这一行:

newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);

也许你知道 C# 上 String.fromCharCode() 的等效方法吗?

【问题讨论】:

  • 你可以做newString += (char) firstChartCode; newString += (char) lastCharCode

标签: c# .net


【解决方案1】:

给你:

public static void Main()
{
    // from 97 to a
    int i = 97;
    Console.WriteLine(StringFromCharCode(97));
}

public static string StringFromCharCode(int code) => ((char)code).ToString();

Demo

请注意,在您的情况下,您可能希望使用 StringBuilder 而不是连接 string

Int to Char in C# 相关(阅读它。它包括一个关于演员与Convert.ToChar 的好评论)

【讨论】:

    【解决方案2】:

    您可以简单地使用Convert.ToChar 方法,Convert.ToChar(97) 将返回a

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2017-02-05
      相关资源
      最近更新 更多