user203042
user203042

Reputation: 111

Reading Hex from an file

So my knowledge of pointers is a bit rusty and I think thats where I'm getting messed up, I am trying to write a function that will grab hex values (an amount n) at a specified offset in the file. And write those values to an array.

File I'm reading from, Example

        0  1  2  3  4  5  6  7      8  9  A  B  C  D  E  F
   0   F6 EA 9D DE D8 40 1C 44     19 24 59 D2 6A 2C 48 1D
   1   FC 96 DE 94 AF 95 FC 42     9B 6D DA 15 D4 CE 88 BB
   2   B8 24 99 8F 65 B5 D3 7E     D9 5D 51 44 89 97 61 85
   3   2D 40 1A DC D5 16 1F 70     84 F9 85 58 C8 0E 13 80
   4   32 AC 10 97 61 B3 16 3B     40 67 7A CA FE E1 4F 2B
   5   21 A9 07 F6 80 26 66 04     20 EC 5C E8 FA 70 68 2C
   6   1C 78 C4 7E 5C DA B9 9C     41 38 66 3F 19 B6 6A 3A

Here's the function I've written thus far.

aDest point's to an array of size nBytes + 1

bAddr point's to firstbyte of the memory region

OffsetAmt is a location which is relative bAddr

nBytes is just the number of bytes that I want to copy

Heres the function

void getHexBytesAt(uint8_t* const aDest, const uint8_t* const bAddr,
                                          uint16_t OffsetAmt, uint8_t nBytes)
{
    const uint8_t *point1 = bAddr; //set he address of point1 to base address  value

//front point1 shift until we get to the specified offset value
    for (int i = 0; i < Offset; i++)
    {
       point1 = (point1 + 1);
    }

//set the values of *aDest to the value of point1;
//increment point1
    for (int k = 0; k < nBytes; k++)
    {
       *aDest = point1;
        point1 = (point1 + 1);
    }   

The problem I'm having is im not even getting the first byte copied into the array correctly,

My output looks like this Getting 9 bytes, starting at the offset 2C

MY OUTPUT: 84 CA CA CA CA CA CA CA CA 
FILE: 89 97 61 85 2D 40 1A DC D5

Upvotes: 0

Views: 142

Answers (1)

harper
harper

Reputation: 13690

If you want to read the data from the Memory bAddr then you must

  • dereference the pointer for reading
  • increment the Destination pointer

This would be implemented like this:

void getHexBytesAt(uint8_t* const aDest, const uint8_t* const bAddr,
                                          uint16_t OffsetAmt, uint8_t nBytes)
{
    const uint8_t *point1 = bAddr; //set he address of point1 to base address  value

//front point1 shift until we get to the specified offset value
    for (int i = 0; i < OffsetAmt; i++)  // fixed typo
    {
       point1 = (point1 + 1);
    }

//set the values of *aDest to the value of point1;
//increment point1
    for (int k = 0; k < nBytes; k++)
    {
       *aDest = *point1;     // copy data from address the point1 points to
        aDest = aDest + 1;   // increment destination pointer
        point1 = (point1 + 1);
    }   
}

But this can be done much simpler:

void getHexBytesAt(uint8_t* const aDest, const uint8_t* const bAddr,
                                          uint16_t OffsetAmt, uint8_t nBytes)
{
    memcpy(aDest, bAddr + OffsetAmt, nBytes);
}

You should consider replacing the function with the one-liner that implements it in your code.

BTW: There is no file used in the code. You should review your question.

Upvotes: 2

Related Questions