Home > Net >  Converting any variable from big to little endian - how to avoid void pointers?
Converting any variable from big to little endian - how to avoid void pointers?

Time:12-15

I'm working on an application that needs to convert any type of the variable from big to little-endian.

My system works with different variable types (16, 32, and 64 bits wide), and I need to be able to change the endianness with a single function. I wrote a function that manages to swap bytes in any variable however, I'm not happy with it. It works, but it requires dereferencing void pointers, which are prone to error with the double star...

  1. Is there any better way to approach the problem?
  2. Is there any way to avoid void pointers as return value? I was thinking about switch-case loop (eg. case 4 bytes -> return int32) however, I don't know how to write a function prototype for a function that returns different values.

My function:

void* swapBytes(void* number, int bytes_num){
    void* swapped;
    unsigned __int8* single_byte_ptr;
    swapped = malloc(bytes_num * sizeof(__int8));

    for (int i = 0; i<bytes_num; i  ){
        single_byte_ptr =((unsigned __int8*)number) i; //get current byte
        *( (__int8*)(swapped) ((bytes_num-1)-i)) = (unsigned __int8)*single_byte_ptr; //save the byte in new position
    }

    return swapped;
}

the way I call this function

__int64 big_number = 35169804487071;
big_number = *(__int64*)(swapBytes(&big_number, 8));

CodePudding user response:

One problem you have is that you're leaking memory. You return a pointer to malloc'ed memory, but you're not saving the pointer when you return.

Given that you're assigning the result back to the same value, you're better off updating the existing variable, swapping the current byte with a byte on the "opposite" side.

You also don't need to use a void * anyplace other than the parameter type. Inside of the function, just use a pointer to an unsigned char or unsigned __int8 to work through the bytes.

void swapBytes(void* number, int bytes_num)
{
    unsigned __int8* ptr = number;
    for (int i = 0; i<bytes_num/2; i  ) {
        unsigned __int8 tmp = ptr[i];
        ptr[i] = ptr[bytes_num-1-i];
        ptr[bytes_num-1-i] = tmp;
    }
}

Then call it like this:

swapBytes(&big_number, sizeof(big_number));

CodePudding user response:

Your solution is very over-engineered and also entirely unsuitable for embedded systems such as MPC57xx.

Any integer type can get safely iterated across using a pointer to character. Assuming uint8_t* is a character type for your compiler, it's as simple as this:

void little_to_big16 (uint8_t       big    [sizeof(uint16_t)], 
                      const uint8_t little [sizeof(uint16_t)])
{
  big[0] = little[1];
  big[1] = little[0];
}

Then write big_to_little16, big_to_little32 etc etc as needed. Such functions can and should probably be inlined too.

Example of use:

#include <stdio.h>
#include <inttypes.h>

void little_to_big16 (uint8_t       big    [sizeof(uint16_t)], 
                      const uint8_t little [sizeof(uint16_t)])
{
    big[0] = little[1];
    big[1] = little[0];
}

int main (void)
{
  uint16_t little = 0xAABB;
  uint16_t big;
  little_to_big16((uint8_t*)&big, (uint8_t*)&little);
  printf("%"PRIx16, big);
}

Output on x86 little endian:

bbaa
  • Related