## Compute Checksum in JavaScript

In this example I'll show how to Compute the result for 8, 16, 32 and 64 bit checksums in JavaScript.

*You can use Notepad.exe or any text editor for creating the pages. You can also do it in Visual Studio by creating a new project and selecting Web / Application.

1. Start off by creating the html page like so:

Code:
```<html>

<title>JavaScript Checksum</title>

<script language="javascript" type="text/javascript">

</script>

<body>

</body>

</html>```

2. Now I will add the JavaScript. In this example I will be placing the script within the "head" tags. If you want it to execute globally then put it in the "head", or else if you only need to use it at run-time then you can put it in the "body".

*All these should be placed within the "javascript" tags.

2a. The first part is the function that computes the checksum result from a given string:

Code:
```    // Compute Checksum:
function Compute(str)
{
var result = 0;
var length = str.length;
for (var i = 0; i < length; i++)
{
var a = str.charAt(i);
var b = a.charCodeAt();
result = (result + b);
}
return result;
}```

2b. This function will convert the decimal number result of the checksum into the hexadecimal form and size it to the correct bits (8, 16, 32, 64). I just did this because hex looks better.

Code:
```    // This function gets the hexadecimal value with specified bits:
function decimalToHex(dec, bits)
{
var hex = Number(dec).toString(16).toUpperCase();
var length = hex.length;
switch(bits)
{
case 8:
if (length < 2)
{
while (length < 2)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 2)
{
while (length > 2)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

case 16:
if (length < 4)
{
while (length < 4)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 4)
{
while (length > 4)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

case 32:
if (length < 8)
{
while (length < 8)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 8)
{
while (length > 8)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

case 64:
if (length < 16)
{
while (length < 16)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 16)
{
while (length > 16)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

default:
hex = null;
break;
}
return hex;
}```

2c. This function prompts the user for text input:

Code:
```    // This function prompts for a text string:
function getString()
{
var str = prompt("Enter some text", "");
return str;
}```

2d. This function does all the work:

Code:
```    // This function runs the whole process:
function showMessage()
{
try
{
var str = getString();
if(str != null && str != "")
{
var message = "Text: " + str;
message += "\n";

var sum = Compute(str);

message += "Checksum8: 0x" + decimalToHex(sum, 8);
message += "\n";

message += "Checksum16: 0x" + decimalToHex(sum, 16);
message += "\n";

message += "Checkusm32: 0x" + decimalToHex(sum, 32);
message += "\n";

message += "Checksum64: 0x" + decimalToHex(sum, 64);

}
}
catch(err)
{
var message = "There was an error on the page.";
message += "\n";
message += "\n";
message += err.description;
}
}```

3. Now that all the JavaScript is in place you need some way to run it, I will be doing this by the "onload" event of the page "body" (aka when the page loads).

Code:
`    <body onload="showMessage();"></body>`

And here is the complete page:

Code:
```<html>

<title>JavaScript Checksum</title>

<!-- Checksum Example -->
<script language="javascript" type="text/javascript">

// Compute Checksum:
function Compute(str)
{
var result = 0;
var length = str.length;
for (var i = 0; i < length; i++)
{
var a = str.charAt(i);
var b = a.charCodeAt();
result = (result + b);
}
return result;
}

// This function gets the hexadecimal value with specified bits:
function decimalToHex(dec, bits)
{
var hex = Number(dec).toString(16).toUpperCase();
var length = hex.length;
switch(bits)
{
case 8:
if (length < 2)
{
while (length < 2)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 2)
{
while (length > 2)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

case 16:
if (length < 4)
{
while (length < 4)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 4)
{
while (length > 4)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

case 32:
if (length < 8)
{
while (length < 8)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 8)
{
while (length > 8)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

case 64:
if (length < 16)
{
while (length < 16)
{
hex = "0" + hex;
length += 1;
}
}
else if (length > 16)
{
while (length > 16)
{
hex = hex.substr(1, hex.length - 1);
length -= 1;
}
}
break;

default:
hex = null;
break;
}
return hex;
}

// This function prompts for a text string:
function getString()
{
var str = prompt("Enter some text", "");
return str;
}

// This function runs the whole process:
function showMessage()
{
try
{
var str = getString();
if(str != null && str != "")
{
var message = "Text: " + str;
message += "\n";

var sum = Compute(str);

message += "Checksum8: 0x" + decimalToHex(sum, 8);
message += "\n";

message += "Checksum16: 0x" + decimalToHex(sum, 16);
message += "\n";

message += "Checkusm32: 0x" + decimalToHex(sum, 32);
message += "\n";

message += "Checksum64: 0x" + decimalToHex(sum, 64);

}
}
catch(err)
{
var message = "There was an error on the page.";
message += "\n";
message += "\n";
message += err.description;