Javascript CharCodeAt c#

All topics about coding, designing, etc. goes in here.
Post Reply
geraldhumphreys
Registered User
Posts: 4
Joined: 27 Mar 2012, 12:01

Javascript CharCodeAt c#

Post by geraldhumphreys » 24 Feb 2016, 08:32

Hi Guys,

I am trying to convert this to c# but i am not winning.

Code: Select all

for (var i = 0; i < plainText.length; i++)
mNumber += (plainText.charCodeAt(i) * (i + 1)) ^ (i + 1);
I cannot get the CharCodeAt in c# and i have hunted for a solution but not coming right.

GDI_Lord
Forum Administrator
Posts: 2663
Joined: 05 Sep 2005, 02:00

Re: Javascript CharCodeAt c#

Post by GDI_Lord » 24 Feb 2016, 09:24

Do you need the unicode value for the character?

Try looking at this:
http://stackoverflow.com/a/7826216/132756

Code: Select all

string text = "Blahblahblah";
int x = text[2]; // Or whatever...

geraldhumphreys
Registered User
Posts: 4
Joined: 27 Mar 2012, 12:01

Re: Javascript CharCodeAt c#

Post by geraldhumphreys » 24 Feb 2016, 11:28

GDI_Lord wrote:Do you need the unicode value for the character?

Try looking at this:
http://stackoverflow.com/a/7826216/132756

Code: Select all

string text = "Blahblahblah";
int x = text[2]; // Or whatever...
I am pretty much trying to convert this to c#.

Code: Select all

function GenerateSerialCode()
		{
			var serialNumber = document.getElementById("serialNumber").value;
			var year = document.getElementById("year").value;
			var month = document.getElementById("month").value;
			var day = document.getElementById("day").value;
			var plainText = serialNumber + year + month + day;

			var magicNumber = 0;
			for (var i = 0; i < plainText.length; i++)
				magicNumber += (plainText.charCodeAt(i) * (i + 1)) ^ (i + 1);

			magicNumber *= 1751873395;
			magicNumber = magicNumber >>> 0; // convert to 32 bit integer

			var magicWord = magicNumber + "";
			var serialCode = "";
			for (var i = 0; i < magicWord.length; i++)
			{
				var c = magicWord.charCodeAt(i);
				if (c < 51)
					serialCode += String.fromCharCode(c + 33);
				else if (c < 53)
					serialCode += String.fromCharCode(c + 62);
				else if (c < 55)
					serialCode += String.fromCharCode(c + 47);
				else if (c < 57)
					serialCode += String.fromCharCode(c + 66);
				else
					serialCode += String.fromCharCode(c);
			}

GDI_Lord
Forum Administrator
Posts: 2663
Joined: 05 Sep 2005, 02:00

Re: Javascript CharCodeAt c#

Post by GDI_Lord » 24 Feb 2016, 14:37

Now why do you need a C# version of the admin password reset tool for Hikvision's cameras? :-)

geraldhumphreys
Registered User
Posts: 4
Joined: 27 Mar 2012, 12:01

Re: Javascript CharCodeAt c#

Post by geraldhumphreys » 24 Feb 2016, 14:53

GDI_Lord wrote:Now why do you need a C# version of the admin password reset tool for Hikvision's cameras? :-)
lol, good you know, i have a project i am busy with and work with hikvision everyday.
I dont have the net all the time so a pc based version will work good for me

GDI_Lord
Forum Administrator
Posts: 2663
Joined: 05 Sep 2005, 02:00

Re: Javascript CharCodeAt c#

Post by GDI_Lord » 24 Feb 2016, 15:04

From https://www.ipcamtalk.com/showthread.ph ... reset-tool
bp2008 wrote:[Here] is the complete source code which you can write to a .html file on your computer, allowing you to use it offline.
According to him/her, create a .html file with the following text inside and then just double click on the file. Have you tried doing this? Then there is no need to convert from JS to C#.

Code: Select all

<html>
<head>
<title>Hikvision Password Reset</title>
<script type="text/javascript">
function padLeft(str, l, c) { str = str + ""; return Array(l - str.length + 1).join(c || " ") + str }
function initialize()
{
document.getElementById("year").value = new Date().getYear() + 1900;
document.getElementById("month").value = padLeft(new Date().getMonth() + 1, 2, '0');
document.getElementById("day").value = padLeft(new Date().getDate(), 2, '0');

document.getElementById("serialNumber").onchange = GenerateSerialCode;
document.getElementById("year").onchange = GenerateSerialCode;
document.getElementById("month").onchange = GenerateSerialCode;
document.getElementById("day").onchange = GenerateSerialCode;
}
function GenerateSerialCode()
{
var serialNumber = document.getElementById("serialNumber").value;
var year = document.getElementById("year").value;
var month = document.getElementById("month").value;
var day = document.getElementById("day").value;
var plainText = serialNumber + year + month + day;

var magicNumber = 0;
for (var i = 0; i < plainText.length; i++)
magicNumber += (plainText.charCodeAt(i) * (i + 1)) ^ (i + 1);

magicNumber *= 1751873395;
magicNumber = magicNumber >>> 0; // convert to 32 bit integer

var magicWord = magicNumber + "";
var serialCode = "";
for (var i = 0; i < magicWord.length; i++)
{
var c = magicWord.charCodeAt(i);
if (c < 51)
serialCode += String.fromCharCode(c + 33);
else if (c < 53)
serialCode += String.fromCharCode(c + 62);
else if (c < 55)
serialCode += String.fromCharCode(c + 47);
else if (c < 57)
serialCode += String.fromCharCode(c + 66);
else
serialCode += String.fromCharCode(c);
}

document.getElementById("output").innerHTML = serialCode;
}
window.onload = initialize;
</script>
<style type="text/css">
body
{
width: 450px;
}
.description
{
margin: 20px 0px;
}
.label
{
margin: 10px 0px;
}
.input
{
margin-bottom: 10px;
}
#output
{
font-weight: bold;
border: 1px solid black;
padding: 10px;
font-size: 2em;
max-width: 100%;
}
</style>
</head>
<body>
<div><h2>Hikvision Camera Password Reset Utility</h2></div>
<div class="description">This tool will generate a <b>password reset code</b> which you may use to reset a forgotten admin password for a Hikvision camera.</div>
<div class="label">Enter your camera's complete CASE SENSITIVE serial number, as seen in the <a href="http://www.google.com/search?q=Hikvision%20SADP">Hikvision SADP</a> tool:</div>
<div class="input"><input type="text" id="serialNumber" style="width: 100%" placeholder="Hikvision Camera Serial Number" /></div>
<div class="label"><b>Important:</b> The date you enter below much match with the camera's clock. <b>Most likely it is not today's date!</b> To find out what date your camera thinks it is, power cycle your camera, give it time to boot up, and then refresh your camera list in SADP and check the Start Time column.</div>
<div class="label">Enter the <b>4 digit</b> year the camera thinks it is:</div>
<div class="input"><input type="text" id="year" style="width: 20%" /></div>
<div class="label">Enter the <b>2 digit</b> month the camera thinks it is:</div>
<div class="input"><input type="text" id="month" style="width: 20%" /></div>
<div class="label">Enter the <b>2 digit</b> day the camera thinks it is:</div>
<div class="input"><input type="text" id="day" style="width: 20%" /></div>
<div class="label">Your <b>password reset code</b> will appear below.</div>
<div id="output"></div>
<div class="label">The code must be entered into the <a href="http://www.google.com/search?q=Hikvision%20SADP">Hikvision SADP</a> tool in the <b>Serial code</b> box (called <b>Security Code</b> in later SADP versions). The camera will compare its internal date and time with the date and time you have entered above. The Serial Number and date much match perfectly or else the code will not work.</div>
</body>
</html>

Post Reply