Thread: reading hex number and add them

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    reading hex number and add them

    Hi.. guys im trying to read a .txt file from c drive. it has hex random values like this
    20 F8 C1 1E 06 EC 95 F4 F8 7E 68 00 F8 61 68 00 20 00 69 77 0B 00 20 FF 69 77 16 00 74 F0 F3 1F
    74 F0 F9 1F 00 77 00 00 26 77 20 00 BD F6 68 77 1D 1E 68 73 4D 1D 63 72 69 00 62 77 0A 00 01 77
    FF FF 00 77 08 00 F8 68 65 00 FB FF F8 61 65 00 04 00 20 F8 EE 1E 00 77 00 01 3D 77 A4 00 73 F0
    56 1E 3D 77 84 00 74 F0 CD 1F 68 48 74 F0 CD 1F 68 49 A8 F2 E8 F0 F8 82 67 00 FF F0 40 F0 00 80
    F8 80 66 00 F8 44 67 00 F8 1A 66 00 74 F0 7E 1F 3D 77 A4 00 73 F0 56 1E 13 77 00 E0 12 77 00 20

    i want to read each line at a time and add all the hex values on that line. each line has 32 hex values....any suggestions..?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sure... look up fscanf() in your compiler's library documentation. There are formatting strings specifically for this task. Adding things up is pretty simple... just translate into an array and add up all the elements.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    6

    reading hex number and add them

    i implemented this code....but if i run this my output file is empty...

    Code:
    while(!inputfile.eof())
    {
    getline(inputfile, Value);
    temp= ArrayCnt % 32;
    if((temp==0)&&(ArrayCnt!=0))
    {
    for(int x=0;x<32;x++)
    {
    chksum+=MYArray[x];
    chksum=chksum*0xFF;
    }
    Number=~chksum;
    Two= Number+1;

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    inputfile.eof is C++ not C... same with getline() ... you need to pick a language and stick to it.

    The rest of your code is basically useless until you can actually read a single line from the file.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by shibo View Post
    i implemented this code....but if i run this my output file is empty...

    Code:
    while(!inputfile.eof())
    {
    getline(inputfile, Value);
    temp= ArrayCnt % 32;
    if((temp==0)&&(ArrayCnt!=0))
    {
    for(int x=0;x<32;x++)
    {
    chksum+=MYArray[x];
    chksum=chksum*0xFF;
    }
    Number=~chksum;
    Two= Number+1;
    There is no outputting of any kind in that code, so an empty output file would be expected.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Hi there....i waana stick in C
    Code:
    FILE * pFile;
          FILE * fp;
           fp = fopen("c:\\MYFlashValues.txt", "r");
           int MYArray[32];
           // Handle file open failure        return;
           if (fp==0)
           {  MessageBox("The file could not be opened!");
              exit(1);
    
    
           else
           {
           int chksum = 0;
           int Valuecount=0;
           char addrs=0;
           char RecordType=0;
           int temp=0;
           char Value;
           short Number;
           short Two;
           int endchksum=0;
           int ArrayCnt=0;
    
           // read data from input stream...
     
                while((ArrayCnt= fgetc( fp ) ) != EOF )
                {
                  temp= ArrayCnt % 32;
                  if((temp==0)&&(ArrayCnt!=0))
                   {
                      for(int x=0;x<32;x++)
                      {
                         chksum+=MYArray[x];
                         chksum=chksum*0xFF;
                      }
    
    pFile = fopen("MYintelhex.txt","a");
    could you pls check how can i read ignoring spaces and add them

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    // untested
    unsigned long int GetChecksum(char *filename)
      {  char buffer[128];
         char *bptr;
         int tmp;
         unsigned int sum;
         FILE *file;
    
         file = fopen(filename,"r");
         if (! file)
            { printf("The file failed to open");
              exit (255); }
    
         while (fgets(buffer,127,file))
           { bptr = buffer;
             while(bptr && (sscanf(bptr, "%x", &tmp) > 0))
                { sum = (sum + temp)  & 0xFFFF; // for 16 bit checksum
                   bptr = strchr(bptr,' '); } }
    
         fclose(file); 
         return sum; }
    Last edited by CommonTater; 12-08-2011 at 10:04 AM.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    Hey thx for your quick reply....
    i want to read one line at a time...until end of file. each line has for example...
    3D 77 84 00 00 FC 09 4A 08 4A 74 F0 F3 1F 74 F0 F9 1F.......................3B [32 different hex value]. after adding all the hex values i want to read next line and do the samething....
    if you could put comments in your code it will be helpfull to under stand...
    Code:
    while
    Code:
     (fgets(buffer,127,file))        { bptr = buffer; 
             while(bptr && (sscanf(bptr, "%x", &tmp) > 0)) 
                { sum = (sum  & 0xFFFF) + tmp; // for 16 bit checksum 
                   bptr = strchr(bptr,' '); } } 

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    6
    and the summing result for each line will be display in hex in a different file...

    Thx alot for ur help....

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shibo View Post
    Hey thx for your quick reply....
    i want to read one line at a time...until end of file. each line has for example...
    3D 77 84 00 00 FC 09 4A 08 4A 74 F0 F3 1F 74 F0 F9 1F.......................3B [32 different hex value]. after adding all the hex values i want to read next line and do the samething....
    if you could put comments in your code it will be helpfull to under stand...
    Code:
    while
    Code:
     (fgets(buffer,127,file))        { bptr = buffer; 
             while(bptr && (sscanf(bptr, "%x", &tmp) > 0)) 
                { sum = (sum  & 0xFFFF) + tmp; // for 16 bit checksum 
                   bptr = strchr(bptr,' '); } } 

    and the summing result for each line will be display in hex in a different file...


    A few things here....

    1) don't post pre-coloured text into the code tags on this forum. Let the forum colour the text itself. That way we can click the to plain text button and copy/paste code to test it.

    2) You are changing the requirements. As you originally stated them the goal was to checksum the file... which I showed you how to do.

    3) I've given you a significant heads up here... don't come begging for more code. Study what I showed you and figure out how to adapt it to your own needs. This is NOT a free code service and I'm not going to write your code for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading large number
    By nimitzhunter in forum C++ Programming
    Replies: 4
    Last Post: 01-21-2011, 12:09 AM
  2. Reading number of digits
    By Mentallic in forum C Programming
    Replies: 7
    Last Post: 03-27-2010, 11:06 PM
  3. reading a number from a file
    By the_head in forum C Programming
    Replies: 2
    Last Post: 10-02-2003, 09:25 PM
  4. Reading files number by number, also getw()?
    By rmullen3 in forum C Programming
    Replies: 4
    Last Post: 01-03-2003, 01:22 PM
  5. Reading in a number backwards!
    By paranormal in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2002, 04:09 AM