cheksum function

C

cranium.2003

hello,
I want a checksum function that operates on unsigned char *
string. Is there any ready function avail to me on linux? Or if not how
to write a function in C that generate cheksum in unsigned short
variable from any length text string?
 
F

Flash Gordon

hello,
I want a checksum function that operates on unsigned char *
string. Is there any ready function avail to me on linux?

The C standard does not include one. For any that Linux may (or may not)
provide you will have to ask in a Linux group.
> Or if not how
to write a function in C that generate cheksum in unsigned short
variable from any length text string?

Iterate over the string and apply whatever checksum algorithm you choose
to each byte. Post your attempt and people will comment on it.
Alternatively post me 250UKP in used notes and I will send you an
implementation by return of post.
 
M

marty

An unsigned char * is not a string in C in the usual sense.

A char * which is terminated by '\0' is a string in C. If you want your
function to operation on an unsigned char * then you'll have to tell it how
long the sequence of bytes are.

unsigned short checkSum(unsigned char *toBeChecked, int toBeCheckedLength);

Martin
 
C

cranium.2003

hello marty,
I am getting unsigned char * from other function. i want to
ask is there any difference to apply strlen on unsigned char * and get
false results as you said that
an unsigned char * is not a string in C in the usual sense.
if it gives correct results then no problem regarding getting and
sending length of that string to checksum function.
Does I require to convert unsigned char * string to any other
data type to checksum it?
 
J

Joe Wright

marty said:
Flash Gordon wrote:




An unsigned char * is not a string in C in the usual sense.

A char * which is terminated by '\0' is a string in C. If you want your
function to operation on an unsigned char * then you'll have to tell it how
long the sequence of bytes are.

unsigned short checkSum(unsigned char *toBeChecked, int toBeCheckedLength);

Martin

No Marty.

The definition of a C string is indeed in terms of char but there is
nothing said about whether char is signed or unsigned. This is an
implementation detail.

Alone among integer types, there are three char types: 1. char, 2.
signed char and 3. unsigned char. The first, char, is identical to one
of the other two. Which one is implementation defined.

ASCII is a 7-bit character set and all its characters will be
'non-negative' even with 'char' being 'signed char'. EBCDIC on the other
hand is an 8-bit character set. For its characters to be 'non-negative'
the implementation of 'char' must be 'unsigned char'.
 
P

Peter Nilsson

marty said:
An unsigned char * is not a string in C in the usual sense.

7.1.1p1:

"A string is a contiguous sequence of characters terminated by and
including the first null character."

There is no mention of strings requiring char type. Indeed character
codings are all positive. It's only when viewed through a signed plain
char (or a signed char) that the bytes have negative values.
A char * which is terminated by '\0' is a string in C.

As is an unsigned char which is terminated by a zero byte.

String comparison functions (for instance) will interpret bytes as
unsigned char values, not char values.
If you want your function to operation on an unsigned char * then
you'll have to tell it how long the sequence of bytes are.

Not if it's null terminated...

size_t foo(const void *msg)
{
return strlen(msg);
}

....or...

size_t foo(const unsigned char *msg)
{
return strlen((const char *) msg);
}
 
B

Ben Pfaff

Joe Wright said:
The definition of a C string is indeed in terms of char but there is
nothing said about whether char is signed or unsigned. This is an
implementation detail.

This is the definition of a C string:

"A string is a contiguous sequence of characters terminated
by and including the first null character."

Thus, the definition of a C string is not in terms of char. It
is in terms of characters. C has three character types, so
elements of a string can have any of those types.
Alone among integer types, there are three char types: 1. char,
2. signed char and 3. unsigned char. The first, char, is identical to
one of the other two. Which one is implementation defined.

It is notable that these are three distinct types, even though
two of them have the same range.
 
M

marty

Not if it's null terminated...
size_t foo(const void *msg)
{
return strlen(msg);
}

...or...

size_t foo(const unsigned char *msg)
{
return strlen((const char *) msg);
}

Yes but how you'll need to start using casts in your program. I compiled
the following with gcc -Wall -pedantic test.c
and it gave me.

test.c: In function `main':
test.c:11: warning: pointer targets in initialization differ in signedness


#include <stdio.h>
#include <string.h>

size_t foo(void *msg)
{
return strlen( msg);
}

int main(int argc, char *argv[])
{
unsigned char *string = "marty was here";
printf("%d\n",foo(string));
return 0;
}

Your correct about strlen not seeming to mind.
My own last post did not appear in my Newsreader (Knode). Don't know what I
done wrong.

Martin.
 
P

Peter Nilsson

marty said:
Yes but how you'll need to start using casts in your program.

The first shows that it isn't. But casts are not always wrong.
I compiled the following with gcc -Wall -pedantic test.c
and it gave me.

test.c: In function `main':
test.c:11: warning: pointer targets in initialization differ in signedness

#include <stdio.h>
#include <string.h>

size_t foo(void *msg)
{
return strlen( msg);
}

int main(int argc, char *argv[])
{
unsigned char *string = "marty was here";

This _requires_ a diagnostic becuase char * and unsigned char * are
not compatible, even though they are convertable.

Try...

unsigned char *string = (void *) "marty was here";

....or...

void *string = "marty was here";
printf("%d\n",foo(string));

%d is not correct for size_t. Since you know the size to be small in
this case, you can simply do...

printf("%u\n", (unsigned) foo(string));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top