LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2008, 07:43 AM   #1
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Rep: Reputation: 0
Calculating checksum without the mod function


I'm using a proprietary language very similar to C, but doesn't have the mod function built in.. Here is what I would normally do to calc checksum:

mod( (sum( all ascii values) / 256) );

Does anyone know of an alternative to the mod function to get the remainder of a division operation?
Thanks,
D
 
Old 06-12-2008, 08:52 AM   #2
ewolf
LQ Newbie
 
Registered: Dec 2007
Location: Chicago, USA
Distribution: Debian, NetBSD
Posts: 25

Rep: Reputation: 15
r = remainder
d = denominator
n = numerator


r = d - n/d

if your language is giving n/d as a decimal or float, you can try casting it as an integer or using a floor() function to trim the decimal.
 
Old 06-12-2008, 08:53 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

Doesn't the % operator work? I never tried a mod function. I can't understand how it can work like you write it. Are you sure you don't mean the fmod function? But it is for floating point numbers.

Floating point: fmod(sum( all ascii values), 256);

Integers: sum(all ascii values) % 256

If the number is 256, you can also write it as:
sum(all ascii values) & 255
 
Old 06-12-2008, 02:02 PM   #4
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
Found something else... be back w/ more info..

Last edited by neutron001; 06-12-2008 at 02:04 PM.
 
Old 06-12-2008, 04:22 PM   #5
neutron001
LQ Newbie
 
Registered: Mar 2008
Posts: 19

Original Poster
Rep: Reputation: 0
This seems to work:
nDenom = 256;
nSum = ascii total;

nQuotient = (nSum / nDenom);
nRemain = nSum - (nDenom * nQuotient);
return(nRemain);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Checksum 4 Slackware download - what type of checksum is this. Earnest Lux Linux - Newbie 1 02-02-2008 08:02 PM
Apache2 mod vhost_alias - problems with .htaccess mod rewrite d_t_baker Linux - Server 1 08-16-2007 07:32 PM
Calculating checksum of a structure george_mercury Programming 3 01-09-2005 05:07 AM
Raw Sockets, Checksum Function Encrypted Programming 13 05-03-2003 03:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration