Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Hex checksum calculator

Go down  Message [Page 1 of 1]

1Hex checksum calculator Empty Hex checksum calculator Mon Jul 31, 2023 1:02 am

marroja

marroja

I was thinking it would be good to have checksum calculator in the utils section of the website.

When programming memories a frequently used file is the Intel .hex format and I thought it would be very useful to have a checksum utility for the last two bytes of each line of the hex file.

2Hex checksum calculator Empty Re: Hex checksum calculator Mon Jul 31, 2023 1:04 am

marroja

marroja

I programmed a small calculator in Java for use "offline" and the code ended being quite compact:

Code:
    public static int checksum(String s){

        int sum = 0;
        s = s.toLowerCase();
        s = s.replaceAll("[^(0-9)|(a-f)]", "");

        //Add the value of every pair of bytes to "sum"
        for (int i = 0; i < s.length(); i += 2) {
            String ss = s.substring(i, i+2);
            sum += Integer.parseInt(ss, 16);
        }

        //If motorola format
        //Just do one's complement
        int res = -sum;

        //If intel format
        //Add one to make it "two's complement"
        // res ++

        //apply mask to the last two bytes
        res = res & 0x00_00_00_ff;

        return res;
    }

arcachofo likes this post

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum