如何在 C 中从字节数组中删除字节?

发布于 2025-01-16 21:26:35 字数 1000 浏览 0 评论 0原文

给定一个长度为 n 的字节数组 buff

unsigned char buff[n] = "....." // len n

我想删除位置 pos 处的 m 个字符,

0< pos,m,pos+m< n

我尝试使用 memmove

memmove(buff + pos, buff + pos + m, n - (pos + m) + 1);

但这并不不适用于字节数组,因为我们没有该 buff 的 '\0' 终止符(但我们知道它的长度)

如何删除之间的字节?任何人请帮忙

编辑:示例输入,

位置  数据
0000  03 00 02 ef 02 f0 80 64 00 08 03 eb 70 82 e0 40
0010  00 ff 30 00 00 00 00 b3 47 43 00 00 00 00 00 00
0020  00 1e 00 c4 00 00 00 00 00 00 00 44 00 65 00 66
0030  00 61 00 75 00 6c 00 74 00 41 00 6c 00 74 00 53

假设我想从数据包中删除突出显示的字节。

新数据包,

位置数据
0000  03 00 02 ef 02 f0 80 64 00 08 03 00 00 00 00 00
0010  00 00 44 00 65 00 66 00 61 00 75 00 6c 00 74 00
0020  41 00 6c 00 74 00 53

Given a byte array buff of length n:

unsigned char buff[n] = "....." // len n

I want to delete m characters at position pos,

0 < pos, m, pos + m < n

I tried using memmove:

memmove(buff + pos, buff + pos + m, n - (pos + m) + 1);

But this doesn't work for byte array as we don't have '\0' terminator for this buff (but we know its length)

How do I delete bytes in between? Anyone please help

Edit: Sample input,

Pos    Data
0000  03 00 02 ef 02 f0 80 64 00 08 03 eb 70 82 e0 40
0010  00 ff 30 00 00 00 00 b3 47 43 00 00 00 00 00 00
0020  00 1e 00 c4 00 00 00 00 00 00 00 44 00 65 00 66
0030  00 61 00 75 00 6c 00 74 00 41 00 6c 00 74 00 53

Say I want to delete the highlighted bytes from packet.

New paket,

Pos    Data
0000  03 00 02 ef 02 f0 80 64 00 08 03 00 00 00 00 00

0010  00 00 44 00 65 00 66 00 61 00 75 00 6c 00 74 00

0020  41 00 6c 00 74 00 53

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

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

发布评论

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

评论(2

手心的海 2025-01-23 21:26:35

这将删除数组中的 m 个字符,而不终止空字符。

int main() 
{
    int pos = 1, m = 3;
    
    unsigned char arr[8] = {1,2,3,4,5,6,7,8};

    memmove(arr + pos, arr + pos + m, sizeof(arr)/sizeof(arr[0]) - pos - m);

  // if required to zero-out the remaining elements
   memset(arr + sizeof(arr)/sizeof(arr[0]) - m, 0, m);

    for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
        printf("%hhu ", arr[i]);
    return 0;
}

输出:1 5 6 7 8 0 0 0

This would erase m chars in an array without terminating null character.

int main() 
{
    int pos = 1, m = 3;
    
    unsigned char arr[8] = {1,2,3,4,5,6,7,8};

    memmove(arr + pos, arr + pos + m, sizeof(arr)/sizeof(arr[0]) - pos - m);

  // if required to zero-out the remaining elements
   memset(arr + sizeof(arr)/sizeof(arr[0]) - m, 0, m);

    for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
        printf("%hhu ", arr[i]);
    return 0;
}

Output: 1 5 6 7 8 0 0 0

泅渡 2025-01-23 21:26:35

您可能想要这样的东西:

#include <stdio.h>
#include <string.h>

void Display(const char buff[], int nb)
{
  for (int i = 0; i < nb; i++)
    printf("%d ", buff[i]);

  printf("\n");
}

int main()
{
  unsigned char buff[20] = { 0,1,2,3,4,5 };
                      // buff has room for 20 elements
  int nbelements = 6; // but there are only 6 meaningful elements

  Display(buff, nbelements);

  int pos = 1;         // delete from element 1
  int nbtodelete = 2;  // delete 2 elements
  memmove(buff + pos, buff + pos + nbtodelete, nbelements - pos - 1);
  nbelements -= nbtodelete;

  Display(buff, nbelements);
}

输出:

0 1 2 3 4 5
0 3 4 5

这是非常不言自明的。

You probably want something like this:

#include <stdio.h>
#include <string.h>

void Display(const char buff[], int nb)
{
  for (int i = 0; i < nb; i++)
    printf("%d ", buff[i]);

  printf("\n");
}

int main()
{
  unsigned char buff[20] = { 0,1,2,3,4,5 };
                      // buff has room for 20 elements
  int nbelements = 6; // but there are only 6 meaningful elements

  Display(buff, nbelements);

  int pos = 1;         // delete from element 1
  int nbtodelete = 2;  // delete 2 elements
  memmove(buff + pos, buff + pos + nbtodelete, nbelements - pos - 1);
  nbelements -= nbtodelete;

  Display(buff, nbelements);
}

Output:

0 1 2 3 4 5
0 3 4 5

It's pretty self explanatory.

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