ZOJ 1164 Software CRC

Source: Internet
Author: User
Tags numeric value

Which uses lots of personal computers. Your boss, Dr Penny Pincher, have wanted to link the computers together for some time but have been unwilling to spend any m Oney on the Ethernet boards you have recommended. You, unwittingly, had pointed out of each of the PCs had come from the vendor with a asynchronous serial port at no ex Tra cost. Dr Pincher, of course, recognizes her opportunity and assigns you the task of writing the software necessary to allow Comm Unication between PCs.

You've read a bit about communications and know that every transmission are subject to error and that the typical solution To this problem are to append some error checking information to the end of each message. This information allows the receiving program to detect when a transmission error had occurred (in most cases). So, off your go to the library, borrow the biggest book on communications you can find and spend your weekend (unpaid overt IME) reading about error checking.

Finally you decide this CRC (cyclic redundancy check) is the best error checking for your situation and write a note to Dr Pincher detailing the proposed error checking mechanism noted below.

The message to being transmitted is viewed as a long positive binary number. The first byte of the message is treated as the most significant byte of the binary number. The second byte is the next most significant, etc. This binary number is called "M" (for message). Instead of transmitting "M" you'll transmit a message, "M2", consisting of "M" followed by a two-byte CRC value.

The CRC value is chosen so, "M2" when divided by a certain 16-bit value "G" leaves a remainder of 0. This makes it easy for the receiving program to determine whether the message have been corrupted by transmission errors. It simply divides any message received by "G". If the remainder of the division is zero, it's assumed that no error has occurred.

You notice this most of the suggested values of "G" in the book is odd, but don ' t see any other similarities, so you Sele CT the value 34943 for "G" (the generator value).


Input

You is to devise a algorithm for calculating the CRC value corresponding to any message that might be sent. To test the algorithm you would write a program which reads lines (each line being all characters up to, but not including The end of line character) as input, and the calculates the CRC value for the message contained D writes the numeric value of the CRC bytes (in hexadecimal notation) on a output line. Each input line would contain no more than 1024x768 ASCII characters. The input is terminated by a line this contains a # in column 1.


Output

CRC in hex format. Note that each CRC printed should is in the range 0 to 34942 (decimal).


Sample Input

This is a test

A
#


Sample Output

About FD
00 00

0C 86

Design an algorithm that calculates the CRC value corresponding to any message that may be sent. To test this algorithm, you will write a program that reads the input rows (each character is a character, but not a newline) as input, each line computes the CRC value of the message contained in that row, and writes the value of the CRC byte (hexadecimal notation) on the output line.

Parse: Calculates all the characters in a row, divided by the remainder of G

The CRC is then computed and, if preceded by 0, the CRC is 0. otherwise, rcr=g-l;

AC Code:

#include <stdio.h>
#define g 34943
int main ()
{
    unsigned char s,flag;
    int cnt;//The number of characters in a row
    //CRC the data structure of
    Union
    {
    unsigned int L;
    struct
    {
        unsigned char B1;
        unsigned char B2;
        unsigned char b3;
        unsigned char b4;

    } CRC;
    } data;
    while (1)
    {
        cnt=0;
        Data. l=0;
        while (scanf ("%c", &s) &&s!= ' \ n ')
        {
            cnt++;
            Flag=s;
            Data. L= (data. l<<8) +s)%g;
        }
        Moves the remainder of the sending information to the integer highest and secondary high byte
        data. L= (data. l<<16)%g;
        if (data. l!=0)
            data. L=g-data. L;
        if (cnt==1&&flag== ' # ') break
            ;
        printf ("%02x%02x\n", (int) data.crc.b2, (int) data.crc.b1);
    }
    return 0;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.