Qt World Summit: Register Today!

Checksum calculation for UDP message which contains QHostAddress

  • Hello All, I have just started learning QT. Part of my QT assignment I am trying to send UDP command to an IP Camera. I am using QHostAddress to assign the IP address of the camera. I would like to know if there is any option of calculating the checksum of the entire udp message in QT. My udp message contains the IP address and the camera specific commands.
    As of now I have calculated the checksum manually and have hard coded it. However I am trying to come up with software code which would do the checksum calculation by itself in QT.
    Also is there any way I could disintegrate the ip assigned using QHostAddress. ?Disintegrate it in bytes ?

  • @NarutoKun

    Hi and welcome to devnet forum

    Checksum is not a single formula used. it is more generic term. It typically depends on specific definitions of algorithms. Therefore you need to more specific what you are using and look into the documentation of Qt.

    What do you mean with disintegration of the IP?

  • @koahnig

    Thank you for the warm welcome.
    In my application I am looking for 8Bit Checksum.

    I am assigning the IP add of the camera to a variable as below.
    IPCam_add = new QHostAddress ("")

    UDP command that I need to write to the socket for message transmission is as follows. --

    Now for 8Bit Checksum calculation I am planning to sum up all the bytes in the UDP message and then take 2'complement. Thought of using a For Loop for summation of all the bytes in the UDP message. However I am not sure how to disintegrate the IP address.
    By disintegrate IP I meant to break down the 4-Bytes IP address into single byte's. So that I can take each byte separately and sum it up, hence calculating the 8Bit Checksum.

    Hoping you understand my requirement if not please let me know.

    Thank you for your time.

  • @NarutoKun

    For the checksum you could go for a QCryptographicHash, if you really like to dig as deep.
    Personally I would not go as far.

    In order to split the ip address into its components it is similar. You could use QHostAddress which allows to use a string as initialization. With toIPv4Address you can get a 4 byte number which you can split into single bytes by standard bit manipulation.

    You can use QString::split and use the dot as separator and form a QStringList. Each component may be transformed by toInt.

    At the day's end the question is if you really want to use Qt for it. Personally I am more a fan of reading from a std::istringstream.

    AFAIK there is no special Qt routine doing that already for you.

  • @koahnig
    I shall try building my application with your suggestions. Will keep you update on the outcome.

    Thanks you for your valuable inputs.

  • @koahnig

    I did the following

    QHostAddress *IPCam_addr;
    int addr;
    IPCam_addr = new QHostAddress ("")
    addr = IPCam_addr->toIPv4Address();
    ip_addr[0] = ((addr >> 24) & 0xFF);
    ip_addr[1] = ((addr >> 16) & 0xFF);
    ip_addr[2] = ((addr >> 8) & 0xFF);
    ip_addr[3] = (addr  & 0xFF);
    qDebug ("%d", ip_addr[0]);
    qDebug ("%d", ip_addr[1]);
    qDebug ("%d", ip_addr[2]);
    qDebug ("%d", ip_addr[3]);

    Output -

    So I am able to access each byte individually. Going forward I shall just sum them up and do a 2's complement to calculate the 8-bit checksum.

    Thank you for your support Koahnig. Good day!!

Log in to reply