React Native – How to Convert Integer to Bytes
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.
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,
- 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.
- 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!
Choose Your Language
- Digital Marketing
- IT Consulting
- Project Management