How to convert this JavaScript to 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>
Please explain me this line in details?
parseInt(form.mac.value.replace(/:/g, '').slice(-6), 16) % 12000;
How to convert this JavaScript to 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>
Please explain me this line in details?
parseInt(form.mac.value.replace(/:/g, '').slice(-6), 16) % 12000;
Share
Improve this question
edited May 21, 2016 at 11:23
user6339740
asked Jun 18, 2015 at 7:50
Muhammad SarbiantoMuhammad Sarbianto
451 gold badge1 silver badge4 bronze badges
1
- calculating a pin based on mac address – Muhammad Sarbianto Commented Jun 18, 2015 at 9:23
3 Answers
Reset to default 1I believe start-to-finish code conversions are a bit out of scope of Stack Overflow. If you posted your non-working C# conversion attempt and asked where it went wrong, I'm sure you'd get a much quicker answer to your first question.
As for your second question:
parseInt(form.mac.value.replace(/:/g, '').slice(-6), 16) % 12000;
translates to:
// 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;
Wrapping javascript function for access using c# instead of conversion/porting
You could look into using Jurassic for doing that.
Calling a JavaScript function from .NET
$(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;
}
})