473,378 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to calculate 2's complement 8 bits checksum ?

I have an array which contain Hex no. in each position.

For examples,
unsigned char data[5];

data[0] = 0x00;
data[1] = 0x01;
data[2] = 0x02;
data[3] = 0xE;
data[4] = 0xEF; --> This is the checksum value

checksum value at data[4] is the checksum calculated from data[0] -
data[3]. I would like to know what is the algorithm to do the 2's
complement for 8 bits checksum, and how can I write the code for it.
I'm still a newbie. Please advise. Thank you.
Nov 13 '05 #1
4 52347
ab*****@yahoo.com (Abby) wrote:
# I have an array which contain Hex no. in each position.
#
# For examples,
# unsigned char data[5];
#
# data[0] = 0x00;
# data[1] = 0x01;
# data[2] = 0x02;
# data[3] = 0xE;
# data[4] = 0xEF; --> This is the checksum value
#
# checksum value at data[4] is the checksum calculated from data[0] -
# data[3]. I would like to know what is the algorithm to do the 2's
# complement for 8 bits checksum, and how can I write the code for it.
# I'm still a newbie. Please advise. Thank you.

One's complement is just complement: ~x.
Two's complement is complement and increment, ignoring carry: (~x)+1.

#include <stdio.h>
int main(int N,char **P) {
static unsigned char data[5] = {0x00,0x01,0x02,0x0E,0};
int i;
for (i=0; i<4; i++) printf("%02x -> %02x (%02x)\n",data[i],
(unsigned char)((~data[i])+1),
(unsigned char)(-data[i]));
return 0;
}

00 -> 00 (00)
01 -> ff (ff)
02 -> fe (fe)
0e -> f2 (f2)

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Quit killing people. That's high profile.
Nov 13 '05 #2

"Abby" <ab*****@yahoo.com> wrote in message

I have an array which contain Hex no. in each position.

I would like to know what is the algorithm to do the 2's
complement for 8 bits checksum, and how can I write the code for it.

Two's complement is a way of representing negative numbers in binary.
Instead of having a bit which acts as a negative "flag", we invert, and then
increment.
So 1 is 0000 0001 int binary
inverting gives 1111 1110
incrementing gives 1111 1111 or -1 in two's complement.

The nice thing is that by adding, and discarding the overflow, we get the
correct answer.

0000 0001 = 1
+1111 1111 = -1
=1 0000 0000

discard the overflow and we get 1 + -1 = 0;

A checksum is a technique to check data for transmission errors or
tampering. If the last few bytes are the sum of all the preceding bytes,
then any errors are likely to be detected.

The problem is that "two's complement checksum" doesn't have a definite
meaning that I'm aware of. It obviously has something to do with taking the
two's complement of a number somewhere, and presumably each byte contributes
to the final answer, but you will have to ask further what exactly is meant
by the term.
Nov 13 '05 #3
Emmanuel Delahaye <em**********@noos.fr> scribbled the following:
In 'comp.lang.c', ab*****@yahoo.com (Abby) wrote:
checksum value at data[4] is the checksum calculated from data[0] -
data[3]. I would like to know what is the algorithm to do the 2's
complement for 8 bits checksum,
How is this a C question? "Add 1 and invert the bits."
No. "Invert the bits and add 1". If you do it the other way around,
you end up with the wrong result. Let's use eight-bit values for
simplicity. The 2's complement of 0 (binary 00000000) should also be 0.
First we add 1 (result binary 00000001) and then we invert the bits:
result binary 11111110. If we do it the right way, we first invert the
bits (result binary 11111111) and then add 1: result binary 00000000,
due to a wrap-around.
and how can I write the code for it.
I'm still a newbie. Please advise. Thank you.

Do your best, and we will check your code.


Certainly his best can be better than your best... =)

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"As we all know, the hardware for the PC is great, but the software sucks."
- Petro Tyschtschenko
Nov 13 '05 #4
Joona I Palaste wrote:

Emmanuel Delahaye <em**********@noos.fr> scribbled the following:
In 'comp.lang.c', ab*****@yahoo.com (Abby) wrote:
checksum value at data[4] is the checksum calculated from data[0] -
data[3]. I would like to know what is the algorithm to do the 2's
complement for 8 bits checksum,

How is this a C question?

"Add 1 and invert the bits."


No. "Invert the bits and add 1".


Subtract 1 and invert the bits, also works.

--
pete
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: wilk | last post by:
I have a problem witch calculating TCP or UDP checksum. This is what I found: u16 in_cksum(u16 *addr,int count) { register long sum = 0; /* add 16-bit words */ while (count > 1) { /* this...
2
by: Abby | last post by:
I need to do 8 bits 2's complement checksum, so I wrote code in order to see if the checksum calculation is correct. ===========================================================================...
1
by: Terry | last post by:
I'm trying to calculate the checksum for UDP packet. The algorithm itself is not difficult (lots of examples out there), but what I'm having the most trouble with is determining the byte order...
24
by: Bob | last post by:
Hi there, I am working on an application to be used in our local Forensics department... Among other things the app will connect to a digital camera and download the images to the hard drive....
15
by: ValK | last post by:
Hello, I’m working with handheld device that communicates with windows service thru the serial port. Transaction between device and my application looks like this: Handshaking: Device sends ...
34
by: Zahid Faizal | last post by:
Kindly suggest a good opensource package (in C or C++) that can compute the checksum of a file. SHA2 would be preferable, but SHA1/SHA0/MD5 would be acceptable as well. We have cards with...
15
by: rjfjohnson | last post by:
Hey, I am trying to communicate with serial device and I need to know the method used to calculate the 1digit checksum. I have a commercial program that can communicate with the device, and a...
3
by: fabianuat | last post by:
Could I have some help calculating a checksum for receive data from a Video server? The data is in Hex and the values and here is an example: 20 24 49 4C 30 31 37 38 37 39 CS CS=Checksum the...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, how can I calculate the checksum (CRC-16) from the byte I retrieve from a hardware device through the RS232 serial port? Christian
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.