473,386 Members | 1,817 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,386 software developers and data experts.

cheksum function

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?

Nov 14 '05 #1
8 2212
cr**********@gmail.com wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #2
Flash Gordon wrote:
cr**********@gmail.com wrote:
hello,
I want a checksum function that operates on unsigned char *
string. Is there any ready function avail to me on linux?


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
Nov 14 '05 #3
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?

Nov 14 '05 #4
marty wrote:
Flash Gordon wrote:

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

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'.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #5
marty wrote:
Flash Gordon wrote:
cr**********@gmail.com wrote:
hello,
I want a checksum function that operates on unsigned char * string. Is there any ready function avail to me on linux?

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);
}

--
Peter

Nov 14 '05 #6
Joe Wright <jo********@comcast.net> writes:
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.
--
"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."
--Michael Girdwood, in the monastery
Nov 14 '05 #7
> 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.

Nov 14 '05 #8
marty wrote:
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.


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));
return 0;
}


--
Peter

Nov 14 '05 #9

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.