C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Checksum Calculation Posted by Lakshmi1984 on 22 Nov 2006 at 10:42 PM
Hi,

I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...

Do suggest me if there are any kind of Examples available for checksum calculation.

Best Regards,
XYZ
Report
Re: Checksum Calculation Posted by stephl on 22 Nov 2006 at 11:04 PM
: Hi,
:
: I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
:
: Do suggest me if there are any kind of Examples available for checksum calculation.
:
: Best Regards,
: XYZ
:
What do you want to print? The bytes in the file?

Steph
Report
Re: Checksum Calculation Posted by Lakshmi1984 on 23 Nov 2006 at 1:25 AM
: : Hi,
: :
: : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: :
: : Do suggest me if there are any kind of Examples available for checksum calculation.
: :
: : Best Regards,
: : XYZ
: :
: What do you want to print? The bytes in the file?
:
: Steph
:
Hi Steph,

Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
But i can only find special characters getting printed & some junk checksum values.


Report
Re: Checksum Calculation Posted by Lundin on 23 Nov 2006 at 2:17 AM
: : : Hi,
: : :
: : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : :
: : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : :
: : : Best Regards,
: : : XYZ
: : :
: : What do you want to print? The bytes in the file?
: :
: : Steph
: :
: Hi Steph,
:
: Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: But i can only find special characters getting printed & some junk checksum values.
:
:
:


It seems like the bug is in the display routine and not in the checksum.
Please post the code.
Report
Re: Checksum Calculation Posted by Lakshmi1984 on 23 Nov 2006 at 2:37 AM
: : : : Hi,
: : : :
: : : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : : :
: : : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : : :
: : : : Best Regards,
: : : : XYZ
: : : :
: : : What do you want to print? The bytes in the file?
: : :
: : : Steph
: : :
: : Hi Steph,
: :
: : Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: : But i can only find special characters getting printed & some junk checksum values.
: :
: :
: :
:
:
: It seems like the bug is in the display routine and not in the checksum.
: Please post the code.
:


Hi Steph... here is the code....


#include <stdio.h>

#include <stdlib.h>

int main(void)

{

    FILE *fp;

    char buf[6];

    /* Open an existing binary file for reading.      */

    if (( fp = fopen ( "C:\\iCube3.3.iic", "rb" ) ) == NULL )

    {

        printf ( "Cannot open file\n" );

        exit ( 1 );

    }

    /* Read characters from the file to the buffer.   */



    fread ( buf, 1, sizeof(buf), fp );

    printf ( "%s\n", buf );



    fclose ( fp );

    return 0;

}
Report
Re: Checksum Calculation Posted by Lundin on 23 Nov 2006 at 2:54 AM
: : : : : Hi,
: : : : :
: : : : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : : : :
: : : : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : : : :
: : : : : Best Regards,
: : : : : XYZ
: : : : :
: : : : What do you want to print? The bytes in the file?
: : : :
: : : : Steph
: : : :
: : : Hi Steph,
: : :
: : : Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: : : But i can only find special characters getting printed & some junk checksum values.
: : :
: : :
: : :
: :
: :
: : It seems like the bug is in the display routine and not in the checksum.
: : Please post the code.
: :
:
:
: Hi Steph... here is the code....
:
:
: 
: #include <stdio.h>
: 
: #include <stdlib.h>
: 
: int main(void)
: 
: {
: 
:     FILE *fp;
: 
:     char buf[6];
: 
:     /* Open an existing binary file for reading.      */
: 
:     if (( fp = fopen ( "C:\\iCube3.3.iic", "rb" ) ) == NULL )
: 
:     {
: 
:         printf ( "Cannot open file\n" );
: 
:         exit ( 1 );
: 
:     }
: 
:     /* Read characters from the file to the buffer.   */
: 
: 
: 
:     fread ( buf, 1, sizeof(buf), fp );
: 
:     printf ( "%s\n", buf );
: 
: 
: 
:     fclose ( fp );
: 
:     return 0;
: 
: }
: 


Yep, as I suspected you are using the wrong print algo. You can't print binary data as text, it won't make sense.

To view binary data as hex, you need to do like this:

int i;

...
for(i=0; i<sizeof(buf); i++)
{
  if(buf[i] < 0x10)
    printf("0%X ", (unsigned char)buf[i]);
  else
    printf("%X ", (unsigned char)buf[i]);
}
Report
Re: Checksum Calculation Posted by Lakshmi1984 on 23 Nov 2006 at 3:11 AM
: : : : : : Hi,
: : : : : :
: : : : : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : : : : :
: : : : : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : : : : :
: : : : : : Best Regards,
: : : : : : XYZ
: : : : : :
: : : : : What do you want to print? The bytes in the file?
: : : : :
: : : : : Steph
: : : : :
: : : : Hi Steph,
: : : :
: : : : Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: : : : But i can only find special characters getting printed & some junk checksum values.
: : : :
: : : :
: : : :
: : :
: : :
: : : It seems like the bug is in the display routine and not in the checksum.
: : : Please post the code.
: : :
: :
: :
: : Hi Steph... here is the code....
: :
: :
: : 
: : #include <stdio.h>
: : 
: : #include <stdlib.h>
: : 
: : int main(void)
: : 
: : {
: : 
: :     FILE *fp;
: : 
: :     char buf[6];
: : 
: :     /* Open an existing binary file for reading.      */
: : 
: :     if (( fp = fopen ( "C:\\iCube3.3.iic", "rb" ) ) == NULL )
: : 
: :     {
: : 
: :         printf ( "Cannot open file\n" );
: : 
: :         exit ( 1 );
: : 
: :     }
: : 
: :     /* Read characters from the file to the buffer.   */
: : 
: : 
: : 
: :     fread ( buf, 1, sizeof(buf), fp );
: : 
: :     printf ( "%s\n", buf );
: : 
: : 
: : 
: :     fclose ( fp );
: : 
: :     return 0;
: : 
: : }
: : 
: 
: 
: Yep, as I suspected you are using the wrong print algo. You can't print binary data as text, it won't make sense.
: 
: To view binary data as hex, you need to do like this:
: 
: 
: int i;
: 
: ...
: for(i=0; i<sizeof(buf); i++)
: {
:   if(buf[i] < 0x10)
:     printf("0%X ", (unsigned char)buf[i]);
:   else
:     printf("%X ", (unsigned char)buf[i]);
: }
: 
: Hi Steph, Thank you. Now m able to read the file... i will proceed with the checksum part & post a que if i have any issues..... Thanks once again.........
Report
Re: Checksum Calculation Posted by stephl on 23 Nov 2006 at 4:32 AM
: : : : : : : Hi,
: : : : : : :
: : : : : : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : : : : : :
: : : : : : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : : : : : :
: : : : : : : Best Regards,
: : : : : : : XYZ
: : : : : : :
: : : : : : What do you want to print? The bytes in the file?
: : : : : :
: : : : : : Steph
: : : : : :
: : : : : Hi Steph,
: : : : :
: : : : : Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: : : : : But i can only find special characters getting printed & some junk checksum values.
: : : : :
: : : : :
: : : : :
: : : :
: : : :
: : : : It seems like the bug is in the display routine and not in the checksum.
: : : : Please post the code.
: : : :
: : :
: : :
: : : Hi Steph... here is the code....
: : :
: : :
: : : 
: : : #include <stdio.h>
: : : 
: : : #include <stdlib.h>
: : : 
: : : int main(void)
: : : 
: : : {
: : : 
: : :     FILE *fp;
: : : 
: : :     char buf[6];
: : : 
: : :     /* Open an existing binary file for reading.      */
: : : 
: : :     if (( fp = fopen ( "C:\\iCube3.3.iic", "rb" ) ) == NULL )
: : : 
: : :     {
: : : 
: : :         printf ( "Cannot open file\n" );
: : : 
: : :         exit ( 1 );
: : : 
: : :     }
: : : 
: : :     /* Read characters from the file to the buffer.   */
: : : 
: : : 
: : : 
: : :     fread ( buf, 1, sizeof(buf), fp );
: : : 
: : :     printf ( "%s\n", buf );
: : : 
: : : 
: : : 
: : :     fclose ( fp );
: : : 
: : :     return 0;
: : : 
: : : }
: : : 
: : 
: : 
: : Yep, as I suspected you are using the wrong print algo. You can't print binary data as text, it won't make sense.
: : 
: : To view binary data as hex, you need to do like this:
: : 
: : 
: : int i;
: : 
: : ...
: : for(i=0; i<sizeof(buf); i++)
: : {
: :   if(buf[i] < 0x10)
: :     printf("0%X ", (unsigned char)buf[i]);
: :   else
: :     printf("%X ", (unsigned char)buf[i]);
: : }
: : 
: : :

:
: Hi Steph,
:
: Thank you. Now m able to read the file... i will proceed with the checksum part & post a que if i have any issues.....
:
: Thanks once again.........
:
I would just like to add that this is Lundin who gave you the solution. I only posted the first message asking you for more information. After that I had a four hour class so I wasn't able to read your answer. But Lundin was. So you should thank Lundin, not me.

Steph
Report
Re: Checksum Calculation Posted by Lakshmi1984 on 23 Nov 2006 at 4:55 AM
: : : : : : : : Hi,
: : : : : : : :
: : : : : : : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : : : : : : :
: : : : : : : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : : : : : : :
: : : : : : : : Best Regards,
: : : : : : : : XYZ
: : : : : : : :
: : : : : : : What do you want to print? The bytes in the file?
: : : : : : :
: : : : : : : Steph
: : : : : : :
: : : : : : Hi Steph,
: : : : : :
: : : : : : Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: : : : : : But i can only find special characters getting printed & some junk checksum values.
: : : : : :
: : : : : :
: : : : : :
: : : : :
: : : : :
: : : : : It seems like the bug is in the display routine and not in the checksum.
: : : : : Please post the code.
: : : : :
: : : :
: : : :
: : : : Hi Steph... here is the code....
: : : :
: : : :
: : : : 
: : : : #include <stdio.h>
: : : : 
: : : : #include <stdlib.h>
: : : : 
: : : : int main(void)
: : : : 
: : : : {
: : : : 
: : : :     FILE *fp;
: : : : 
: : : :     char buf[6];
: : : : 
: : : :     /* Open an existing binary file for reading.      */
: : : : 
: : : :     if (( fp = fopen ( "C:\\iCube3.3.iic", "rb" ) ) == NULL )
: : : : 
: : : :     {
: : : : 
: : : :         printf ( "Cannot open file\n" );
: : : : 
: : : :         exit ( 1 );
: : : : 
: : : :     }
: : : : 
: : : :     /* Read characters from the file to the buffer.   */
: : : : 
: : : : 
: : : : 
: : : :     fread ( buf, 1, sizeof(buf), fp );
: : : : 
: : : :     printf ( "%s\n", buf );
: : : : 
: : : : 
: : : : 
: : : :     fclose ( fp );
: : : : 
: : : :     return 0;
: : : : 
: : : : }
: : : : 
: : : 
: : : 
: : : Yep, as I suspected you are using the wrong print algo. You can't print binary data as text, it won't make sense.
: : : 
: : : To view binary data as hex, you need to do like this:
: : : 
: : : 
: : : int i;
: : : 
: : : ...
: : : for(i=0; i<sizeof(buf); i++)
: : : {
: : :   if(buf[i] < 0x10)
: : :     printf("0%X ", (unsigned char)buf[i]);
: : :   else
: : :     printf("%X ", (unsigned char)buf[i]);
: : : }
: : : 
: : : : :

: :
: : Hi Steph,
: :
: : Thank you. Now m able to read the file... i will proceed with the checksum part & post a que if i have any issues.....
: :
: : Thanks once again.........
: :
: I would just like to add that this is Lundin who gave you the solution. I only posted the first message asking you for more information. After that I had a four hour class so I wasn't able to read your answer. But Lundin was. So you should thank Lundin, not me.
:
: Steph
:


Thank U Lundin....
Report
Re: Checksum Calculation Posted by stephl on 23 Nov 2006 at 4:54 AM
: : : : : : Hi,
: : : : : :
: : : : : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : : : : :
: : : : : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : : : : :
: : : : : : Best Regards,
: : : : : : XYZ
: : : : : :
: : : : : What do you want to print? The bytes in the file?
: : : : :
: : : : : Steph
: : : : :
: : : : Hi Steph,
: : : :
: : : : Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: : : : But i can only find special characters getting printed & some junk checksum values.
: : : :
: : : :
: : : :
: : :
: : :
: : : It seems like the bug is in the display routine and not in the checksum.
: : : Please post the code.
: : :
: :
: :
: : Hi Steph... here is the code....
: :
: :
: : 
: : #include <stdio.h>
: : 
: : #include <stdlib.h>
: : 
: : int main(void)
: : 
: : {
: : 
: :     FILE *fp;
: : 
: :     char buf[6];
: : 
: :     /* Open an existing binary file for reading.      */
: : 
: :     if (( fp = fopen ( "C:\\iCube3.3.iic", "rb" ) ) == NULL )
: : 
: :     {
: : 
: :         printf ( "Cannot open file\n" );
: : 
: :         exit ( 1 );
: : 
: :     }
: : 
: :     /* Read characters from the file to the buffer.   */
: : 
: : 
: : 
: :     fread ( buf, 1, sizeof(buf), fp );
: : 
: :     printf ( "%s\n", buf );
: : 
: : 
: : 
: :     fclose ( fp );
: : 
: :     return 0;
: : 
: : }
: : 
: 
: 
: Yep, as I suspected you are using the wrong print algo. You can't print binary data as text, it won't make sense.
: 
: To view binary data as hex, you need to do like this:
: 
: 
: int i;
: 
: ...
: for(i=0; i<sizeof(buf); i++)
: {
:   if(buf[i] < 0x10)
:     printf("0%X ", (unsigned char)buf[i]);
:   else
:     printf("%X ", (unsigned char)buf[i]);
: }
: 

:
It's a remark of less importance, but I think the above test could be replaced by:
for (i=0;i<sizeof buf;++i)
 printf("%02X ",(unsigned char) buf[i]);


Steph
Report
Re: Checksum Calculation Posted by Lakshmi1984 on 23 Nov 2006 at 5:19 AM
: : : : : : : Hi,
: : : : : : :
: : : : : : : I have written a routine for checksum calculation (XOR). In this I will be reading a binary file. But when I try to read a binary file , i see that only special characters get printed at the output. How do I proceed with it. Please Help me in resolving the issue...
: : : : : : :
: : : : : : : Do suggest me if there are any kind of Examples available for checksum calculation.
: : : : : : :
: : : : : : : Best Regards,
: : : : : : : XYZ
: : : : : : :
: : : : : : What do you want to print? The bytes in the file?
: : : : : :
: : : : : : Steph
: : : : : :
: : : : : Hi Steph,
: : : : :
: : : : : Thank you very much for the quick response. Yes I want to print the bytes in a file, & also XOR each byte & print the final checksum value.
: : : : : But i can only find special characters getting printed & some junk checksum values.
: : : : :
: : : : :
: : : : :
: : : :
: : : :
: : : : It seems like the bug is in the display routine and not in the checksum.
: : : : Please post the code.
: : : :
: : :
: : :
: : : Hi Steph... here is the code....
: : :
: : :
: : : 
: : : #include <stdio.h>
: : : 
: : : #include <stdlib.h>
: : : 
: : : int main(void)
: : : 
: : : {
: : : 
: : :     FILE *fp;
: : : 
: : :     char buf[6];
: : : 
: : :     /* Open an existing binary file for reading.      */
: : : 
: : :     if (( fp = fopen ( "C:\\iCube3.3.iic", "rb" ) ) == NULL )
: : : 
: : :     {
: : : 
: : :         printf ( "Cannot open file\n" );
: : : 
: : :         exit ( 1 );
: : : 
: : :     }
: : : 
: : :     /* Read characters from the file to the buffer.   */
: : : 
: : : 
: : : 
: : :     fread ( buf, 1, sizeof(buf), fp );
: : : 
: : :     printf ( "%s\n", buf );
: : : 
: : : 
: : : 
: : :     fclose ( fp );
: : : 
: : :     return 0;
: : : 
: : : }
: : : 
: : 
: : 
: : Yep, as I suspected you are using the wrong print algo. You can't print binary data as text, it won't make sense.
: : 
: : To view binary data as hex, you need to do like this:
: : 
: : 
: : int i;
: : 
: : ...
: : for(i=0; i<sizeof(buf); i++)
: : {
: :   if(buf[i] < 0x10)
: :     printf("0%X ", (unsigned char)buf[i]);
: :   else
: :     printf("%X ", (unsigned char)buf[i]);
: : }
: : 

: :
: It's a remark of less importance, but I think the above test could be replaced by:
:
: for (i=0;i<sizeof buf;++i)
:  printf("%02X ",(unsigned char) buf[i]);
: 

:
: Steph
:


Hi Steph.......

Well here is another problem...... can v not print the binary file in 1's & 0's format ????????? i tried it out, but its not printing in 1's & 0's format....
Report
Re: Checksum Calculation Posted by stephl on 23 Nov 2006 at 5:40 AM
: Hi Steph.......
:
: Well here is another problem...... can v not print the binary file in 1's & 0's format ????????? i tried it out, but its not printing in 1's & 0's format....
:
Hi!

There's no format specification for printf to print numbers in binary.
However, it's easy to convert from hex to binary or vice versa:
0000  0
0001  1
0010  2
0011  3
0100  4
0101  5
0110  6
0111  7
1000  8
1001  9
1010  A
1011  B
1100  C
1101  D
1110  E
1111  F


A simple code could be (not tested):
unsigned int mask;

...
for (i=0;i<sizeof buf;++i)
 {
 for (mask=1u<<7;mask;mask>>=1)
  printf("%d",buf[i]&mask?1:0);
 printf(" ");
 }


Steph
Report
Re: Checksum Calculation Posted by Lakshmi1984 on 23 Nov 2006 at 9:09 PM
: : Hi Steph.......
: :
: : Well here is another problem...... can v not print the binary file in 1's & 0's format ????????? i tried it out, but its not printing in 1's & 0's format....
: :
: Hi!
:
: There's no format specification for printf to print numbers in binary.
: However, it's easy to convert from hex to binary or vice versa:
:
: 0000  0
: 0001  1
: 0010  2
: 0011  3
: 0100  4
: 0101  5
: 0110  6
: 0111  7
: 1000  8
: 1001  9
: 1010  A
: 1011  B
: 1100  C
: 1101  D
: 1110  E
: 1111  F
: 

:
: A simple code could be (not tested):
:
: unsigned int mask;
: 
: ...
: for (i=0;i<sizeof buf;++i)
:  {
:  for (mask=1u<<7;mask;mask>>=1)
:   printf("%d",buf[i]&mask?1:0);
:  printf(" ");
:  }
: 

:
: Steph
:

ok Steph......ill test it n inform you... thank you.....



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.