Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 832
  • Last Modified:

VB6 How do you calculate the checksum and split it into 2 bytes the first high a 2nd low?

We have 22 bytes to send via rs232 to a device usign vb6, the first byte we think is the length of the message. The last two bytes we think are reserved for the checksum.

How do you calculate the checksum for this and split it into 2 bytes the first high a 2nd low?
1 Solution
Jose ParrotGraphics ExpertCommented:
There are several ways to calculate a checksum. The simplest one is to add all the bytes together, considering just the lower byte. For example, supose we have a message with three bytes (in hex) :    45  7A   BE
The sum is 45 + 7A +  BE  =  17D, so the checksum is hex 7D.
It is up to you to define how you want the checksum. Of course it can be 5 bytes long if you want.
To send such checksum and the message together, in general we sen in the following sequence:
1. Message lenght.
    If yours is somethig around 20 bytes, then a single byte is enough.
     To minimize risks of future messages to not fit this limitationm you can use 2 bytes
2. Message body
     As many bytes you have informed before.
3. Checksum
     If your purpose is only a simple way to check if the message iwas sent correctly,
     a simple byte is enough. If you want to be more rigorous, use two bytes.
     If you want more than a simple checking, for example, you want a very secure
     checking, you can use a hash, which is a bit more complicated than just add bytes.
Of course, the important thing is to observe the same criteria for both sides, the sender and the receiver.
When sending a two byte number, you decide which byte send first: the lower or the upper. Again, the important is to use the same method to send and to receive.
To split a number greater than 255 (and smaller than 65535) into two bytes, we make:

To view details on how to send/receive data via RS232, you should use the COM port. The tutorial at http://www.thaiio.com/prog-cgi/0005_vb.html
covers well this subject.

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now