computing a 8 bit checksum in c++ -
i have following setup trying write custom file header. have fields described follows:
// assume these variables initialized. unsigned short x, y, z; unsigned int dl; unsigned int offset; // end assume u_int8_t major_version = 0x31; u_int8_t minor_version = 0x30; u_int_32_t data_offset = (u_int_32_t)offset; u_int16_t size_x = (u_int16_t)x; u_int16_t size_y = (u_int16_t)y; u_int16_t size_z = (u_int16_t)z; u_int_32_t data_size = (u_int_32_t)dl;
now, compute 8 bit header checksum fields starting major revision data_size variables. new , simple suffice current needs.
i'm not sure trying achieve, strictly answer question, 8-bit checksum computed as
sum_of_all_elements %255
simply put, add elements together, , sum % 255 checksum.
watch out overflows when doing addition (you can compute partial sums if run trouble).
on side note, 8-bit checksum not - won't distinguish cases , won't data recovery; that's why 16-bit checksum preferred.
Comments
Post a Comment