Cable Forum

Cable Forum (https://www.cableforum.uk/board/index.php)
-   General IT Discussion (https://www.cableforum.uk/board/forumdisplay.php?f=19)
-   -   Calling all Programmers please (https://www.cableforum.uk/board/showthread.php?t=18110)

Mauldor 29-09-2004 11:34

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

philip.j.fry 29-09-2004 13:25

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(...)

}


gary_580 29-09-2004 13:45

Re: Calling al Programmers plese
 
Take a look here, good example and explanation too

http://www.codeproject.com/cpp/checksum.asp

punky 29-09-2004 14:08

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

bob_a_builder 29-09-2004 14:14

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()

Mauldor 29-09-2004 19:28

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 - :)


All times are GMT. The time now is 19:02.

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