What is it?

We are working on a React Native app for monitoring a Wifi device. The app connects with the device over Wifi and communicates over UDP sockets. As its UDP, the time it can take to receive a response is not fixed. The response is a binary stream where each data point could be unsigned short, single, unsigned int etc. We are expected to read the data and calculate a checksum which is to be validated against the response checksum provided to ensure response’ correctness.

The issue is, Javascript (we are using React-native to build the app) does not have unsigned short and when we calculate the checksum it generates a 32 bit integer! So, how do we convert this 32bit integer to two bytes?

How to convert 32bit integer to two bytes?

The UDP socket response has two bytes of checksum value, we need to calculate the checksum and then convert it to two bytes and then validate against the response checksum. We need to follow the steps below,

  1. First of all make sure that the checksum message is perfect, if it contains a single less / more values than expected then the calculated value will not match.
  2. For example, the calculated checksum is 1513135. To convert it to two bytes, see the below code with explanation.

This way, we got the two bytes from the calculated checksum integer and now able to validate this. Seems the real programming đź™‚ !

Consulting is free â€“ let us help you grow!