0

I want to move through a hexdump one byte at a time, using a pointer, until I find a specific sequence of bytes that is X bytes long. To do this, I need to cast a pointer to a size of X bytes. For example, a pointer for a size of 3 bytes. I know that I could simply use something like uint16_t if I wanted it to be 2 bytes, or uint32_t if I wanted it to be 4 bytes. But neither of these work for this.

I have to start by pointing to the start of the block of memory that I have the location of, so that I can move through it one byte at a time. How can I do this without losing that position?

1 Answers1

0

This is not possible. If your system doesn't have any types that use 3 bytes of storage then you cannot have a pointer to such a type. Instead you should use an unsigned char * and read bytes out of where it is pointing to, and do something with them.

For example if those 3 bytes are the least significant 3 bytes of an int, in big-endian format, you could write:

unsigned char *ptr = /* find the right location */;

unsigned int foo = ptr[0] * 0x10000u + ptr[1] * 0x100u + ptr[2];
M.M
  • 138,810
  • 21
  • 208
  • 365