CRC-CCITT convertir Kermit 16 Code DELPHI à C #
Question
Je travaille sur une fonction qui va me donner une valeur Kermit CRC à partir d'une chaîne de HEX. J'ai un morceau de code dans DELPHI. Je suis un développeur .NET et besoin du code en C #.
function CRC_16(cadena : string):word;
var
valuehex : word;
i: integer;
CRC : word;
Begin
CRC := 0;
for i := 1 to length(cadena) do
begin
valuehex := ((ord(cadena[i]) XOR CRC) AND $0F) * $1081;
CRC := CRC SHR 4;
CRC := CRC XOR valuehex;
valuehex := (((ord(cadena[i]) SHR 4) XOR LO(CRC)) AND $0F);
CRC := CRC SHR 4;
CRC := CRC XOR (valuehex * $1081);
end;
CRC_16 := (LO(CRC) SHL 8) OR HI(CRC);
end;
J'obtenu le code de cette page: Kermit CRC dans DELPHI
Je suppose que la fonction Delphi est correcte. Si quelqu'un peut s'il vous plaît convertir le code en C # qui sera grande. J'ai essayé de convertir en C #, mais suis perdu dans WORD type de données et la fonction LO de Delphi. Merci à vous tous.
La solution
De forums MSDN :
static long ComputeCRC(byte[] val)
{
long crc;
long q;
byte c;
crc = 0;
for (int i = 0; i < val.Length; i++)
{
c = val[i];
q = (crc ^ c) & 0x0f;
crc = (crc >> 4) ^ (q * 0x1081);
q = (crc ^ (c >> 4)) & 0xf;
crc = (crc >> 4) ^ (q * 0x1081);
}
return (byte)crc << 8 | (byte)(crc >> 8);
}
Encoding.ASCII < a href = "http://msdn.microsoft.com/en-us/library/system.text.encoding.getbytes.aspx" rel = "nofollow noreferrer">. GetBytes (chaîne) pour convertir un string
à un byte[]
.
Autres conseils
Un mot est un entier non signé de 16 bits (qui peut stocker les valeurs 0..65535).
Lo renvoie l'octet de poids faible d'un nombre entier. Donc, si l'entier est 0x7B41AF, par exemple, lo retournera 0xAF.