计算c中的校验和 [英] to calculate checksum in c

查看:499
本文介绍了计算c中的校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个程序,用来收集用户在朗姆酒的输入..

这样的

输入字符串:abc1234f

并且程序必须做计算为ab + c1 + 23 + 4f

并将结果返回两个字符

a program which takes input from user at rum tym..
like this
enter the string : abc1234f
and program must do calculations as ab+c1+23+4f
and return the result in two characters

推荐答案

我们不做你的作业:它是因为一个原因而定。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是你想的很难!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


这很简单,你必须:

That's pretty easy, you have to:
  1. 0初始化你的校验和,比如 chk = 0;
  2. 从输入字符串中取两个字符,例如ab
  3. 将两个字符转换为 unsigned char ,比如 u 使用 strtoul [ ^ ], base 参数设置为 16 (十六进制数)
  4. 将获得的 unsigned char 与校验和相加: chk + = u;
  5. 除非字符串完成,否则转到第2点
  1. initialize you checksum with 0, say chk=0;
  2. Take two characters at time from the input string, e.g. "ab"
  3. Convert the two characters to a unsigned char, say u using strtoul[^] with base parameter set to 16 (hexadecimal number)
  4. sum the obtained unsigned char to the checksum: chk += u;
  5. Go to point 2 unless the string is completed





值得没有:

  • unsigned char 的总和丢弃任何进位(按校验和算法的要求)
  • 您也可以使用自己手工制作的双十六进制字符到字节函数合并步骤2,3


  • It is worth nothing:

    • The sum on unsigned char discards any carry (as required by checksum algorithm)
    • You may also merge steps 2,3 possibly using your own hand crafted two-hex-characters-to-byte function

    • 这篇关于计算c中的校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆