【问题标题】:Convert JavaScript to C#将 JavaScript 转换为 C#
【发布时间】:2015-06-18 07:50:31
【问题描述】:

如何将此 JavaScript 转换为 C#?

<script>
  function zeroPad(num, places) {
    var zero = places - num.toString().length + 1;
    return Array(+(zero > 0 && zero)).join("0") + num;
  }

    var accum = 0;
    var pin = parseInt(form.mac.value.replace(/:/g, '').slice(-6), 16) % 12000;
    var p = pin;
    while (pin)
      accum = (((accum + (3 * (pin % 10))) | 0) + (((pin / 10) | 0) % 10)) | 0, pin = ((pin / 100) | 0);
    accum = ((10 - accum % 10) % 10);
    form.pin.value = (zeroPad(p, 7) + "" + accum);
  }
</script>

请详细解释一下这条线?

parseInt(form.mac.value.replace(/:/g, '').slice(-6), 16) % 12000;

【问题讨论】:

  • 根据mac地址计算pin

标签: javascript c# code-conversion


【解决方案1】:

我相信从头到尾的代码转换有点超出 Stack Overflow 的范围。如果您发布了您的非工作 C# 转换尝试并询问它哪里出错了,我相信您会更快地得到第一个问题的答案。

关于你的第二个问题:

parseInt(form.mac.value.replace(/:/g, '').slice(-6), 16) % 12000;

翻译为:

// Gets some mac address from some object outside the code you posted
var MACAddrString = form.mac.value;
// Delete the :'s between MAC address bytes
MACAddrString = MACAddrString.replace(/:/g, '');
// Take the last 3 bytes (6 hex digit symbols)
MACAddrString = MACAddrString.slice(-6);
// Parse the hex string to a number. Second argument indicates base 16.
var MACAddrInt = parseInt(MACAddrString, 16);
// Calculate the pin
var pin = MACAddrInt % 12000;

【讨论】:

  • 非常感谢juho,....你能用文字帮助这个功能吗? function zeroPad(num, places) { var zero = places - num.toString().length + 1; return Array(+(zero > 0 && zero)).join("0") + num; }
  • 如何从 MAC 地址中删除 ':' ?
【解决方案2】:

包装javascript函数以使用c#而不是转换/移植来访问

你可以考虑使用侏罗纪来做到这一点。

Calling a JavaScript function from .NET

【讨论】:

    【解决方案3】:
     $(document).ready(function () { StartCountDown(); }); //start the countdown
    
    function Decrement() {
        currentMinutes = Math.floor(secs / 60);
        currentSeconds = secs % 60;
        if (currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
        secs--;
        document.getElementById("timerText").innerHTML = "Time Remaining " + currentMinutes + ":" + currentSeconds;
        if (secs !== -1) {
            setTimeout('Decrement()', 1000);
        }
        else {
            window.location.href = "default.aspx?timeout=1"
        }
    }
    
    function CheckIfAllQuestionAnswerHasBeenSubmitted() {
        var numItems = $('.tblOptions').length;
        var flagtocheckcount = 0;
        $(".tblOptions").each(function () {
            var groupname = $('input[type=radio]:first', this).attr('name');
            if (!$("input[name='" + groupname + "']:checked").val()) {
                $(this).parents(".tableclass").addClass("border");
                var tableid = $(this).closest('table [class^="tableclass"]').attr("id");
            }
            else {
                flagtocheckcount = flagtocheckcount + 1;
            }
        })
    

    【讨论】:

    • 拜托,你能用更详细的解释来扩展你的答案吗?这对理解非常有用。谢谢!
    猜你喜欢
    • 2013-11-01
    • 2021-10-27
    • 2011-05-11
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多