如何在 C# 中复制 Perl 的解包功能?

发布于 2024-07-07 03:01:23 字数 396 浏览 7 评论 0 原文

我正在尝试在 C# 中重新创建 Perl 脚本,但在创建目标系统所需的校验和值时遇到问题。

在 Perl 中,此校验和是使用 unpack 函数计算的:

while (<PACKAGE>) {
    $checksum += unpack("%32C*", $_);
}
$checksum %= 32767;
close(PACKAGE);

其中 PACKAGE 是 .tar 文件输入流

我需要在 C# 中复制它,但找不到复制方法unpack 函数。

感谢所有帮助!

(我知道有更好的校验和计算可用,但无法更改目标系统,因此无法更改计算)

I am trying to recreate a Perl script in C# but have a problem creating a checksum value that a target system needs.

In Perl this checksum is calculated using the unpack function:

while (<PACKAGE>) {
    $checksum += unpack("%32C*", $_);
}
$checksum %= 32767;
close(PACKAGE);

where PACKAGE is the .tar file input stream

I need to replicate this in C# but can't find a means of replicating that unpack function.

All help appreciated!

(I know there are much better checksum calculations available but can't change target system so can't change calculation)

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

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

发布评论

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

评论(5

护你周全 2024-07-14 03:01:23

Mono 中似乎有一个名为 DataConvert 的库,它的编写目的是提供类似于 Perl 包的功能/解压。 这能满足您的需要吗?

There seems to be a library in Mono called DataConvert that was written to provide facilities similar to Perl's pack/unpack. Does this do what you need?

书间行客 2024-07-14 03:01:23

Perkl 的解包在此处此处。 由此您应该能够用 C# 编写等效的代码。

Perkl's unpack is described here and here. From that you should be able to write an equivalent in C#.

锦欢 2024-07-14 03:01:23

为了补充 Mitch Wheat 的评论,这里有一个 Java 实现(仅执行单个块)。 我相信您会找到一种方法将其转换为 C#,并执行多个块。

int sum = 0;
for (byte b : buffer) {
    sum += (int) b & 255;
}
return sum % 32767;

希望这可以帮助!

To supplement Mitch Wheat's comment, here's a Java implementation (which does a single block only). I'm sure you'll find a way to convert it into C#, and to do multiple blocks.

int sum = 0;
for (byte b : buffer) {
    sum += (int) b & 255;
}
return sum % 32767;

Hope this helps!

魂牵梦绕锁你心扉 2024-07-14 03:01:23

在我的测试中,使用 %32C 解包似乎是字节的加和,限制为 32 位。

print unpack("%32C*", 'A');
65
print unpack("%32C*", 'AA');
130

重现这一点应该不难。

In my testing here, unpack with %32C appears to be the additive sum of the bytes, limited to 32 bits.

print unpack("%32C*", 'A');
65
print unpack("%32C*", 'AA');
130

It shouldn't be hard to reproduce that.

十年九夏 2024-07-14 03:01:23

根据 Chris Jester-Young 和 piCookie 的评论,我开发了以下功能。 希望对你有帮助。

int fileCheckSum(const char *fileName)
{
   FILE *fp;
   long fileSize;
   char *fileBuffer;
   size_t result;
   int sum = 0;
   long index;

   fp = fopen(fileName, "rb");
   if (fp == NULL)
   {
      fputs ("File error",stderr); 
      exit (1);
   }

   fseek(fp, 0L, SEEK_END);
   fileSize = ftell(fp);
   fseek(fp, 0L, SEEK_SET);

   fileBuffer = (char*) malloc (sizeof(char) * fileSize);   
   if (fileBuffer == NULL)
   {
      fputs ("Memory error",stderr);
      exit (2);
   }

   result = fread(fileBuffer, 1, fileSize, fp);   
   if (result != fileSize)
   {
      fputs ("Reading error", stderr);
      if (fileBuffer != NULL)
         free(fileBuffer);

      exit (3);
   }

   for (index = 0; index < fileSize; index++)
   {
      sum += fileBuffer[index] & 255;
   }

   fclose(fp);
   if (fileBuffer != NULL)
      free(fileBuffer);

   return sum % 32767;  
}

Based on the comments of Chris Jester-Young and piCookie, I developed the following function. I hope you find it useful.

int fileCheckSum(const char *fileName)
{
   FILE *fp;
   long fileSize;
   char *fileBuffer;
   size_t result;
   int sum = 0;
   long index;

   fp = fopen(fileName, "rb");
   if (fp == NULL)
   {
      fputs ("File error",stderr); 
      exit (1);
   }

   fseek(fp, 0L, SEEK_END);
   fileSize = ftell(fp);
   fseek(fp, 0L, SEEK_SET);

   fileBuffer = (char*) malloc (sizeof(char) * fileSize);   
   if (fileBuffer == NULL)
   {
      fputs ("Memory error",stderr);
      exit (2);
   }

   result = fread(fileBuffer, 1, fileSize, fp);   
   if (result != fileSize)
   {
      fputs ("Reading error", stderr);
      if (fileBuffer != NULL)
         free(fileBuffer);

      exit (3);
   }

   for (index = 0; index < fileSize; index++)
   {
      sum += fileBuffer[index] & 255;
   }

   fclose(fp);
   if (fileBuffer != NULL)
      free(fileBuffer);

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