0

I have a block of memory represented as a collection of hex nybbles. This is viewed in a hex dump format where the value of the left column is the first two hex digits of the offset in that row. The column heading for each byte is the last digit of that offset. It looks something like this (I abbreviated some parts to save time/space):

  0  1  2  3 ..... 9  A  B .... F
0 AD 1E 08 ......................
1 .......
. .......
. .......
9 .......
A ....... 
. .......
F .......
10.......
11.......
. .......
1E.. DE
1F.......

I want to go through that block of memory to a specific byte using a pointer. I then want to print the hex representation of that offset (for example, DE would be at offset 1E1). My code right now looks like:

   uint8_t *p = baseAddr;//create a pointer that points to the first byte of memory in the array
   for(int i = 0; i < Length; i++){//use a for loop to move p from the start to the maximum length of the specified region 
       if(*p == Byte){//if it matches the byte you're looking for
           fprintf(Out, "%02X ", p);//print the location of the current byte
       }
       p++;
   }

But instead of printing out the correct value, it prints out something like "800419B1" and gives me a warning that says "format '02X' expects type 'unsigned int' but argument 3 has type 'uint8_t *'"

Is the offset something I should be tracking as I go, or is it something I can get from the pointer? If so, how do I do it?

1 Answers1

2

Subtract the base address from p to get the offset:

fprintf(Out, "%02X ", p - baseAddr);

You don't actually need p, you can just use i:

for (int i = 0; i < Length; i++) {
    if (baseAddr[i] == Byte) {
        fprintf(Out, "%02X ", i);
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612