如何在 C 中从字节数组中删除字节?
给定一个长度为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将删除数组中的
m
个字符,而不终止空字符。输出:
1 5 6 7 8 0 0 0
This would erase
m
chars in an array without terminating null character.Output:
1 5 6 7 8 0 0 0
您可能想要这样的东西:
输出:
这是非常不言自明的。
You probably want something like this:
Output:
It's pretty self explanatory.