Forum Articles
  Welcome back Join CF
You are here You are here: Home | Forum | Calling all Programmers please

You are currently viewing our boards as a guest which gives you limited access to view most of the discussions, articles and other free features. By joining our Virgin Media community you will have full access to all discussions, be able to view and post threads, communicate privately with other members (PM), respond to polls, upload your own images/photos, and access many other special features. Registration is fast, simple and absolutely free so please join our community today.


Welcome to Cable Forum
Go Back   Cable Forum > Computers & IT > General IT Discussion
Register FAQ Community Calendar

Calling all Programmers please
Reply
 
Thread Tools
Old 29-09-2004, 11:34   #1
Mauldor
Inactive
 
Mauldor's Avatar
 
Join Date: Jan 2004
Location: Scunthorpe
Age: 59
Services: 50mbit, Base TV, Base Phone
Posts: 437
Mauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of light
Calling all Programmers please

I wonder if some could write me a very simple program in any Prog lang you care to - what i need is a program to take a text file - add up all the ascii codes for each char and return a Sum at the end - either in Hex or Dec (prefer Hex)...

Im working on something and trying to work out of this is the method for a check sum there using - ive searched google and not found anything as yet ..
Mauldor is offline   Reply With Quote
Advertisement
Old 29-09-2004, 13:25   #2
philip.j.fry
Inactive
 
philip.j.fry's Avatar
 
Join Date: Jul 2003
Posts: 1,395
philip.j.fry has reached the bronze age
philip.j.fry has reached the bronze agephilip.j.fry has reached the bronze agephilip.j.fry has reached the bronze agephilip.j.fry has reached the bronze age
Re: Calling al Programmers plese

Here's something written in Java, it shows the Hex an Dec values. To run just provide the name of the file you want to scan as the first command line agument, e.g.

java check_sumthing <filename>

Note that things like spaces and new line characters count towards the total.


Code:
import java.io.*;


public class check_sumthing
{
	
	private static FileReader infile = null;
	private static BufferedReader instream = null;
	private static int finalint = 0;

	public static void main(String[] args)
	{
		
		if(args.length == 0)
		{
			System.out.println("No filename provided\nUsage: \"java check_sumthing <filename>\"");
		}
		
		try
		{
			infile = new FileReader(args[0]);
			instream = new BufferedReader(infile);
			
			int in;
			while ((in = instream.read()) != -1)
			{
				finalint += in;
			}
			instream.close();
		}
		catch(IOException e)
		{
			System.out.println("There was an error reading from the file, did you give the correct filename?");
		}
		
		System.out.println("Dec: " + finalint);
		System.out.println("Hex: " + Integer.toHexString(0x10000 | finalint).substring(1).toUpperCase());	
		
	}//end main(...)

}
philip.j.fry is offline   Reply With Quote
Old 29-09-2004, 13:45   #3
gary_580
Guest
 
Posts: n/a
Re: Calling al Programmers plese

Take a look here, good example and explanation too

http://www.codeproject.com/cpp/checksum.asp
  Reply With Quote
Old 29-09-2004, 14:08   #4
punky
Inactive
 
Join Date: Jun 2003
Age: 44
Posts: 14,750
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
Re: Calling all Programmers please

I have been beaten to it it seems (damn it, I wrote it so i'll post it at least), but I have knocked up something quick and dirty (in C) here:

Code:
#include <stdio.h>
#include <sys\file.h>

int main(int argc, char *argv[])
{

  int fd ;
  int checksum = 0;
  char chr[1];

  fd = open(argv[1],O_RDONLY);   //open file
    if (fd == -1)
    {
    printf("Unable to open file");
    }
    else
    {

    while(read(fd,chr,1)==1) //read each char at a time til failure (EOF)
    {
    checksum += chr[0];  //add ASCII value to cumlative value
    }
    }

    printf("Checksum in decimal: %d\n", checksum);  //display total in decimal
    printf("Checksum in hex: %X", checksum);  //display total in hexadecimal
    close(fd);

  return 0;
}
And you call it like this (after compile and linking, assuming the output is called checksum, of course)

checksum filename

If you need me to add more comments in it, or explain anything, let me know. It needs a bit more work (check there is a parameter in the command line), but it does the job.

The psuedo-code is more or less the same, which is why it looks so close to Phil's version. Basically:

1. Open a file
2. Read one character at a time until you get a null value/error
3. Add the ascii value (normally cast character to an integer) to a cumlative value
4. format and display the integer as a hexadecimal value.

HTH
punky is offline   Reply With Quote
Old 29-09-2004, 14:14   #5
bob_a_builder
Inactive
 
Join Date: Dec 2003
Posts: 503
bob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to beholdbob_a_builder is a splendid one to behold
Re: Calling all Programmers please

I prefer to keep it simple

function CheckSum(str) {
var sum;
sum=0;
i = 0
while (i<str.length) {
sum = sum + str.charCodeAt(i);
i=i+1;
}
return sum;
}

charCodeAt(i); is just teh javascript equiv of basic mid() and asc()
bob_a_builder is offline   Reply With Quote
Old 29-09-2004, 19:28   #6
Mauldor
Inactive
 
Mauldor's Avatar
 
Join Date: Jan 2004
Location: Scunthorpe
Age: 59
Services: 50mbit, Base TV, Base Phone
Posts: 437
Mauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of lightMauldor is a glorious beacon of light
Re: Calling all Programmers please

Many thanks for the files/info - Ill give them a Try and see if i can crack this damm checksum thing - its uses 8 numbers and is not time / date or file size based which is why i thought sum total of all the ascii -
Mauldor is offline   Reply With Quote
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 On
HTML code is Off

Forum Jump


All times are GMT. The time now is 20:54.


Server: lithium.zmnt.uk
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
All Posts and Content are © Cable Forum