More elegant alternative (* (uint16_t *) [[NSData] bytes])?

I need the bytes to change some bytes representing an unsigned integer of 16 bits. The following code works, but seems a little ridiculous:

CFSwapInt16(*(uint16_t*)[[myNSDataVariable] bytes]);

Here is what I deduced:

[[myNSDataVariable] bytes] returns a void pointer to an array of bytes.

(uint16_t*)outputs a void pointer to a pointer uint16_t.

The final external *plays the pointer uint16_t, providing access to the actual value uint16_t.

Is there a more elegant way to get value uint16_tfrom NSData?

+4
source share
3 answers

I would say that the following is more readable:

uint16_t *valuePtr = (uint16_t *)[myNSDataVariable bytes];
uint16_t swappedValue = CFSwapInt16(*valuePtr);

-. , , :

uint16_t SwappedIntegerFromData(NSData *data) {
    uint16_t *valuePtr = (uint16_t *)[data bytes];
    return CFSwapInt16(*valuePtr);
}

uint16_t value = SwappedIntegerFromData(myNSDataVariable);

, -.

+5

.

, (Objective-) C , 2- , .

, :

typedef uint16_t *UInt16Ptr;

UInt16Ptr, * . bytes . :

CFSwapInt16(*(UInt16Ptr)myNSDataVariable.bytes);

, , .

+3

- C, " ":

void  swap_bytes( uint16_t  *p_u )
{
    char  *p_b = (char *) p_u;

    char  saved = 0[ p_u ];
    0[ p_u ]    = 1[ p_u ];
    1[ p_u ]    = saved;
}

, UINTATHERIUM uint16_t? , ?

-2

All Articles