首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS解密方法转换为C#

JS解密方法转换为C#
EN

Stack Overflow用户
提问于 2021-10-15 12:15:45
回答 2查看 56关注 0票数 2

我需要一些JS代码翻译到C#的帮助。JS代码:

代码语言:javascript
复制
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#上绑定了翻译:

代码语言:javascript
复制
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;
                }
            }

        }

但我如何翻译这行:

代码语言:javascript
复制
newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode);

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-15 12:44:57

这就是了:

代码语言:javascript
复制
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#相关(请阅读。它包含了一个关于cast和Convert.ToChar的很好的评论)

票数 1
EN

Stack Overflow用户

发布于 2021-10-15 13:17:36

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69584570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档