Question

I have this problem. I have a payload that dinamically change its length. If I don't cut this payload, I will have also informations that are rubbish for me. So I want to cut the payload in base of my payload length.

I have this part of code:

uint64_t data_tmp = HTONLL((uint64_t) zp->data);
uint8_t len = zp->length;
uint64_t mask = 0xFFFF >> (64 - len);
data_tmp = data_tmp & mask;
printf("DATA_TMP = %llu\n", data_tmp);

zp->data is after written into a t->payload field. So this is my payload. It's a 64 bit field.

I want to temporary take this payload. Calculate my payload length, that is the zp->length field, of type uint8. After I would create a mask that shift of 64 byte - payload_length in the way that I can then apply it to the temporary payload and obtain a field with only data I want and all zeros.

How can I do this?

For example. If payload_length is 01 and payload 00+..............(rubbish) I want obtain a payload like 00000000 on 64 byte.

If my payload_length is 13 and payload 1284732089322309.... I want obtain only thirtheen bytes.

Sorry if it's difficult to say...

Was it helpful?

Solution 2

You need this:

uint64_t mask = (1uLL << (len * 4)) - 1;
data_tmp = data_tmp & mask;

This will give you the lowest len of 4-bit digits. If you need highest instead, you can rotate data_tmp before applying the mask:

data_tmp >>= (64 - len * 4)

OTHER TIPS

Not sure if i understood your question correctly, but you use logical AND instead of binary AND.

expression "variable && variable" evaluates to 1 or 0

expression "variable & variable" evaluates to binary AND which, with your proper mask, will crop the data as you propably intended

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top