You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS解密方法转C#,String.fromCharCode等效实现及代码问题求助

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;
        }
    }
}

疑问解答

1. 对应代码行的翻译

newString += String.fromCharCode(firstCharCode) + String.fromCharCode(lastCharCode); 对应的C#写法为:

newString += Convert.ToChar(firstCharCode).ToString() + Convert.ToChar(lastCharCode).ToString();

2. String.fromCharCode的C#等效实现

C#中Convert.ToChar(int value)方法与JS的String.fromCharCode()功能完全一致,作用是将指定的UTF-16字符编码转换为对应的字符。

3. 现有代码需要修正的问题

  • 错误使用Regex.Escape:Regex.Escape是用于转义正则表达式特殊字符的方法,和JS的escape功能完全不同,JS的escape是URI转义类方法,C#中对应等效实现为Uri.EscapeDataString。
  • Substring参数错误:C#的Substring第二个参数是截取长度,不是结束索引,你写的codeStr.Substring(codeStr.Length - 2, codeStr.Length)会报错,应该改为codeStr.Substring(codeStr.Length - 2, 2)。
  • 缺失字符串拼接逻辑:ch>132的分支中没有将转换后的两个字符拼接到newString中,也缺失了ch<=132时的else分支,没有把原字符拼接到结果中。
  • 缺失返回值:方法最后没有return结果字符串newString。

修正后的完整C#代码

using System;
using System.Web;

private string Decription(string encryptedText)
{
    string ft = Uri.EscapeDataString(encryptedText);
    string text = HttpUtility.UrlDecode(ft);
    string newString = "";

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

内容的提问来源于stack exchange,提问作者Vitalii Feduniak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 12:45:05