checksum的编程实现

checksum的编程实现,第1张

///凳樱嫌 <summary>

/// 获取校验和,ASCII编码

/// </summary>

/// <param name="s"枣手></param>

/// <param checkSum="s"></param>

#region 获取校验和,ASCII编码

/// <summary>

/// 获颂首取校验和,ASCII编码

/// </summary>

/// <param name="s"></param>

/// <param checkSum="s"></param>

private bool GetCheckSum( string s , out string checkSum )

{

checkSum = ""

if ( 0 == s.Length )

return false

int sIndex = s.IndexOf( '$' ) + 1

int eIndex = s.IndexOf( "**" )

string str = ""

if ( sIndex <1 || eIndex <1 )

str = s

else

str = s.Substring( sIndex , eIndex - sIndex )

byte [] cs = Encoding.ASCII.GetBytes( str )

//核心部分

for ( int i = 0 i <cs.Length - 1 i++ )

{

cs [i + 1] = (byte) ( cs [i] ^ cs [i + 1] )

}

checkSum = cs [cs.Length - 1].ToString( "x2" ).ToUpper()

//checkSum = Convert.ToString( cs [cs.Length - 1] , 16 ).ToUpper()

return true

}

#include <stdio.h>

#include <烂宏stdlib.h>

int main()

{

char ch

signed char checksum=-1

while((ch=getchar())!=EOF)

{

putchar(ch)

checksum+=ch

if(ch=='\n')

{

printf("%d\饥蠢册档没n",checksum)

checksum=-1

}

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8193388.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存