算法题:UVA 128 Software CRC(数论 进制转化)

Software CRC

You work for a company which uses lots of personal computers. Your boss, Dr Penny Pincher, has wanted to link the computers together for some time but has been unwilling to spend any money on the Ethernet boards you have recommended. You, unwittingly, have pointed out that each of the PCs has come from the vendor with an asynchronous serial port at no extra cost. Dr Pincher, of course, recognizes her opportunity and assigns you the task of writing the software necessary to allow communication between PCs.

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

Finally you decide that 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.

CRC Generation

The message to be 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 will be called ``m'' (for message). Instead of transmitting ``m'' you will transmit a message, ``m2'', consisting of ``m'' followed by a two-byte CRC value.

The CRC value is chosen so that ``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 has been corrupted by transmission errors. It simply divides any message received by ``g''. If the remainder of the division is zero, it is assumed that no error has occurred.

You notice that most of the suggested values of ``g'' in the book are odd, but don't see any other similarities, so you select the value 34943 for ``g'' (the generator value).

Input and Output

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

Sample Input

this is a test

A

#

Sample Output

77 FD

00 00

0C 86

时间: 2024-09-13 07:42:12

算法题:UVA 128 Software CRC(数论 进制转化)的相关文章

UVa 128 Software CRC:模计算及CRC循环冗余校验码

128 - Software CRCTime limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=64 You work for a company which uses lots of personal computers. Your boss, Dr Penny Pi

函数-C语言中关于进制转化问题

问题描述 C语言中关于进制转化问题 请问如何用htoi函数将十六进制组成的字符串转化成十进制数(前面可能包含0X或0x) 解决方案 C语言 进制的转化黑马程序员---C语言-进制问题c语言的进制问题 解决方案二: int htoi(const char *s) { if( !s )return 0; if( *s == '0' ) { s++; if( *s == 'x' || *s == 'X' )s++; } int n = 0; while( *s ) { n <<= 4; if( *s

Delphi实现把10进制转换成16进制的函数进制转化

  delphi中有直接把10进制转换成16进制的函数: function IntToHex(Value: Integer; Digits: Integer): string; overload;  function IntToHex(Value: Int64; Digits: Integer): string; overload; unit uConversion; interface uses SysUtils,Math; type TConversion = class public //

16进制显示字节流技巧分享_java

用UE的人都会觉得16进制显示文件灰常方便.为啥捏?当你要对文件加密.转码.编码的时候,蹦出一堆01二进制看着都会头大.毕竟十六进制显示文件简短方便.至少中考高考时涂过卡吧,1+2+4+8能算明白是几吧.当然,那些中考和高考都能把1248码都涂错的童鞋们,一看就知道它们果断与程序猿这个"神剩"的职业无缘哈-- 因为之前试着参加过科普创新大赛,当时做的咚咚是把文件以字节流读入,并转化成二进制.四进制.十六进制字符串,然后刷的一下子输出到控制台.再根据每个位的值,分别以2色.4色.16色的

浅议Oracle中的进制转换

oracle|转换 作者: Eygle 出处: BLOG 进制转换是开发中经常需要用到的,本文简单介绍几种常用的进制转化方法. 一 16进制转换为10进制 可以通过to_number函数实现 SQL> select to_number('19f','xxx') from dual; TO_NUMBER('19F','XXX') ---------------------- 415 SQL> select to_number('f','xx') from dual; TO_NUMBER('F',

sql-SQL 10进制转35进制 求解释

问题描述 SQL 10进制转35进制 求解释 SQL 10进制转35进制 求解释 在调查使用SQL进行10进制转35进制转化的资料时,找到如下SQL文. 经过运行确实能成转化,但小弟才疏学浅实在看不懂原理,请教各位大神帮忙解释一下 重要步骤的原理或目的. 谢谢!!!!!!! DECLARE @BASE_35 VARCHAR(100) DECLARE @BASE_10 INT SET @BASE_10 = '88' SET @BASE_35=N'' SELECT @BASE_35 = CASE W

sql-SQL 10进制转16进制 求解释

问题描述 SQL 10进制转16进制 求解释 SQL 10进制转16进制 求解释 在调查使用SQL进行10进制转35进制转化的资料时,找到如下SQL文. 经过运行确实能成转化,但小弟才疏学浅实在看不懂原理,请教各位大神帮忙解释一下 重要步骤的原理或目的. 谢谢!!!!!!! DECLARE @BASE_35 VARCHAR(100) DECLARE @BASE_10 INT SET @BASE_10 = '88' SET @BASE_35=N'' SELECT @BASE_35 = CASE W

JS中的进制转换以及作用_javascript技巧

js的进制转换, 分为2进制,8进制,10进制,16进制之间的相互转换, 我们直接利用 对象.toString()即可实现: 运行下面代码 //10进制转为16进制 (10).toString(16) // =>"a" //8进制转为16进制 (012).toString(16) // =>"a" //16进制转为10进制 (0x16).toString(10) // =>"22" //16进制转为8进制 (0x16).toSt

javascript各种进制转换方法

开发中,我们一般遇到进制的转换,也无非是下面几种:2进制,8进制,10进制,16进制.今天分享一下在JavaScript中,怎么进行各种进制转换的方法. 我们直接利用 对象.toString()即可实现: //10进制转为16进制 (10).toString(16) // =>"a" //8进制转为16进制 (012).toString(16) // =>"a" //16进制转为10进制 (0x16).toString(10) // =>"