求几个delphi crc16转C#代码

netboygg 2014-08-01 08:27:06
现做一个项目,需要用到如下方法,但是要转成C#的。非常感谢了!
function mjComCRC(buf: PByte;len: Integer): Word;
var
i,j: Integer;
chr: Word;
begin
Result := $ffff;
for j:=0 to len-1 do begin
chr := PByte(LongWord(buf)+j)^ and $ff;
Result := Result xor chr;
for i:=0 to 8 -1 do begin
if ((Result and 01)<>0) then
Result := (Result shr 1) xor $a001
else
Result := Result shr 1;
end;
Result := Result and $ffff;
end;
end;



function crc16(str11:string):string;
var
str,Str1,str2: String;
Ret: Word;
mybyte:pchar;
i: integer;
begin
Str1 := str11; //假定你用Edit1输入
SetLength(Str,Length(Str1));
i := HexToBin(PChar(str1),PChar(Str),Length(Str1));
SetLength(Str,i);
Ret := mjComCRC(pbyte(Str),i);
GetMem(mybyte,sizeof(Ret) * 2);
BinToHex(PChar(@Ret),PChar(mybyte),sizeof(ret));
str2:=copy(strpas(mybyte),1,4);
FreeMem(mybyte,sizeof(Ret) * 2);
result:= copy(str2,1,2)+copy(str2,3,2);
end;


function HexStrToStr(const S:string):string;
//16进制字符串转换成字符串
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;


function strtohexstr(const S:string):string;
//字符串转换成16进制字符串
var
I:Integer;
begin
for I:=1 to Length(S) do
begin
if I=1 then
Result:=IntToHex(Ord(S[1]),2)
else Result:=Result+IntToHex(Ord(S[I]),2);
end;
end;

...全文
111 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
devmiao 2014-08-02
  • 打赏
  • 举报
回复
google site:download.csdn crc16 C# 有很多
Ptrtoptr 2014-08-01
  • 打赏
  • 举报
回复
关键是我看不懂delphi啊,soory

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧