# JavaScript SIP2 Checksum Calculation in Javascript

I'm working on a REST interface to a library system that uses the SIP2 protocol (https://en.wikipedia.org/wiki/Standard_Interchange_Protocol) and was able to get things working on a system that doesn't require error correction without a problem. However, my code is now talking to another system that requires checksums, described as so in the specification:

"To calculate the checksum add each character as an unsigned binary number, take the lower 16 bits of the total and perform a 2's complement. The checksum field is the result represented by four hex digits."

I've taken a few runs at this but not matter what I do I can't get a checksum back that matches my example message. I'm probably making this harder than it should be (seems like it would be easier in a lower-level language with proper binary types, etc.). Here's my latest attempt:

``````var checksum = 0;

// add each character as an unsigned binary number
for(var i=0;i<message.length;i++){
checksum += message[i].charCodeAt();
}

console.log("character sum: " + checksum);

// take the lower 16 bits of the total
checksum = checksum.toString(2);
console.log("character sum binary representation: " + checksum);
while(checksum.length < 16){
checksum = "0" + checksum;
}
checksum = checksum.substr(0,16);
console.log("lower 16 bits of character total: " + checksum);

// convert to dec
checksum = parseInt(checksum,2);
console.log("checksum dec: " + checksum);

// perform 2's complement
checksum = (checksum  & 0xFFFF) * -1;
console.log("2s complement: " + checksum.toString(2));

// convert to 4 hex digits
checksum = dec2hex(checksum);
console.log("checksum hex: " + checksum);

function dec2hex(i) {
return (i+0x10000).toString(16).substr(-4).toUpperCase();
}
``````

The expected checksum for the string above is "F39A".

## How to acces variable from one function to another in javascript

I want to access the value of variable a inside var2() function. However, it gives an error saying the value is not defined. <html> <head> <script type="text/javascript"> function ...

I want to access the value of variable a inside var2() function. However, it gives an error saying the value is not defined. <html> <head> <script type="text/javascript"> function ...

1. access variable from another class java
2. access variable from another class c#
3. access variable from another function python
4. access variable from another class python
5. access variable from another method java
6. access variable from another file python
7. access variable from another class swift
8. access variable from parent class python
9. access variable from main method java
10. access variable from class python
11. access variable from inner class java
12. access variable from parent class java
13. access variable from another js file
14. access variable from header file c++
15. access variable from another method c#
16. access variable from another php file
17. access variable from function python
18. access variable from outside function javascript
19. access variable from another script unity
20. access variable from another activity android

## Angular how to fire a callback function after DOM elements are updated for each digest

I want to make an angular body-scroll only table. To make it simpler, I created two tables, one of which only has the headers, and the other table is for the body. The body table is wrapped into a div ...

I want to make an angular body-scroll only table. To make it simpler, I created two tables, one of which only has the headers, and the other table is for the body. The body table is wrapped into a div ...

1. angularfire callback

## Removing empty strings from array

I understand there are other pages on this but I am trying to get my own working and I do not know why it is not working. I am new to node.js. for (var index in output) { if (opt.options....

I understand there are other pages on this but I am trying to get my own working and I do not know why it is not working. I am new to node.js. for (var index in output) { if (opt.options....

1. removing empty strings from a list in python
2. removing empty strings from list
3. remove empty strings from array javascript
4. remove empty strings from list c#
5. remove empty strings from array
6. remove empty strings from array ruby
7. remove empty strings from array php
8. remove empty strings from array java
9. remove empty strings from array js
10. remove empty strings from list java
11. remove empty strings from array c#
12. remove empty strings from array python
13. remove empty strings from list r
14. remove empty strings from vector r
15. remove empty strings from object javascript
16. remove empty strings python
17. remove empty strings r
18. remove empty strings from list javascript
19. remove empty strings
20. remove empty strings from array swift

## jQuery UI Sortable: order of events

Does anyone know in which order the jQuery sortable events are being triggered? I'm asking this because I had a problem with stop and update. It made more sense to me that update event would come ...

Does anyone know in which order the jQuery sortable events are being triggered? I'm asking this because I had a problem with stop and update. It made more sense to me that update event would come ...

1. jquery sortable order number
2. jquery sortable order
3. jquery sortable order save
4. jquery sortable reorder list
5. jquery sortable reorder array
6. jquery sortable get order
7. jquery sortable save order in database
8. jquery sortable update order
9. jquery sortable get order of elements
10. jquery sortable set order
11. jquery sortable save order in database mvc
12. jquery sortable get order of items
13. jquery sortable change order
14. jquery ui sortable order
15. jquery sortable change order programmatically
16. jquery sortable save order mysql
17. jquery sortable post order
18. jquery sortable sort order
19. jquery sortable event order
20. jquery ui sortable order number
1