将 CRC-CCITT Kermit 16 DELPHI 代码转换为 C#

发布于 2024-09-01 06:17:42 字数 795 浏览 14 评论 0原文

我正在开发一个函数,该函数将从十六进制字符串中给出 Kermit CRC 值。我有一段DELPHI代码。我是一名 .NET 开发人员,需要 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;

我从这个网页得到了代码: Kermit CRC in DELPHI

我猜Delphi函数是正确的。如果有人可以将代码转换为 C#,那就太好了。我尝试转换为C#,但迷失在WORD数据类型和Delphi的LO函数中。谢谢大家。

I am working on a function that will give me a Kermit CRC value from a HEX string. I have a piece of code in DELPHI. I am a .NET developer and need the code in 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;

I got the code from this webpage: Kermit CRC in DELPHI

I guess that Delphi function is correct. If any one can please convert the code to C# that will be great. I tried to convert to C#, but got lost in WORD data type and the LO function of Delphi. Thank you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥繁 2024-09-08 06:17:42

来自 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.GetBytes(string) 转换 字符串字节[]

From MSDN forums:

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);
}

Use Encoding.ASCII.GetBytes(string) to convert a string to a byte[].

荒芜了季节 2024-09-08 06:17:42

字是一个 16 位无符号整数(可以存储值 0..65535)。

Lo 返回整数的低位字节。例如,如果整数是 0x7B41AF,lo 将返回 0xAF。

A word is a 16-bit unsigned integer (which can store the values 0..65535).

Lo returns the low-order byte of an integer. So if the integer is 0x7B41AF, for example, lo will return 0xAF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文