Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve value from bytes stored in a byte array?

I am trying to store 8 bytes in a byte array to store the value of a pointer.

int main() {
    unsigned long a = 0;
    char buf[8];
    int i = 0;
    int *p = &i;
    a = (unsigned long)p;

    while (i < 8)
    {
        buf[i] = (a >> (8 * i)) & 0xFF;
        i++;
    }
    a = 0;
    i = 0;
    while (i < 8)
    {
        a = ?
        i++;
    }
    p = (int *)a;
}

The first loop stores successive bytes of p, as casted into usigned long in a, but I don't know how to retrieve the value in the second loop. Does somebody have a clue?

like image 551
qleguennec Avatar asked Nov 27 '25 22:11

qleguennec


1 Answers

This is the inverse code to yours:

a = 0;
while (i < 8)
{
    a |= ((unsigned long)buf[i] & 0xff ) << (8 * i);
    i++;
}
like image 178
Rabbid76 Avatar answered Nov 29 '25 10:11

Rabbid76



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!