Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - get chars from part of byte array

How can I get char array or char pointer from part of byte array? Let´s say I have variable-size string in byte array which begins at 18 bytes and ends 4 bytes from end of array. How can I get this?

Edit: And what about dot? I should have dots in that byte array but when I copied by memcpy I get string without dots. How can I fix this?

like image 806
Libor Zapletal Avatar asked Mar 05 '26 21:03

Libor Zapletal


1 Answers

To extract a part of an array, you can use memcpy.

#include <string.h>

char dst[4];

/* Here, we can assume `src+18` and `dst` don't overlap. */
memcpy(dst, src + 18, 4);

C11 (n1570), § 7.24.2.1 The memcpy function
The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

like image 83
md5 Avatar answered Mar 08 '26 14:03

md5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!