How can I change the byte order of an integer in c?

My task is to convert the data file from one end to the other (from the big end to the small one and vice versa) using C. I look online for about 3 hours for other examples and read my tutorial, however I am so fixated on how to start this function . So far, my order of events is correct (from 1 to 4), but inside my function "convert_and_save" I have to create a char array using → char buffer [4]; Can someone please help me? even if you just let me know what to see, I would really appreciate it.

I need to write a function:

void convert_and_save (structure record element, FILE * output_handle, int number);

inside this function, follow these steps:

(1) Convert an integer to an array of characters using:

    int integer_to_characters(int number, char * buffer)
    {
       memcpy(buffer, &number, 4);
    }

(2) Reorder the character order in this array.

(3) Convert the array of characters back to an integer using:

    int characters_to_integer(char * buffer)
    {
       int result;
       memcpy(&result, buffer, 4);
       return result;
    }

(4) write the converted record to the output file using:

    void save_record(FILE * file_handle, struct record a)
    {
       char output_buffer[size_of_record];
       integer_to_characters(a.age, &(output_buffer[0]));
       memcpy(&(output_buffer[4]), a.name, 12);
       integer_to_characters(a.department, &(output_buffer[16]));
       fwrite(output_buffer, sizeof(char), size_of_record, file_handle);
    }
+4
source share
5 answers

This is one of the features that I wrote for Parrot VM, you can download byteorder.c from parrotcode.org. There are probably shorter ways to do this, but it works for different integers, and we have a macro to detect the platform byte in PARROT_BIGENDIAN, you can drop all of this. Also, as mentioned above, you can find htonl (), which is nop on bigandian hardware, but converts to small endian (just take the x86 Linux implementation)

INTVAL
fetch_iv_le(INTVAL w)
{
    ASSERT_ARGS(fetch_iv_le)
#if !PARROT_BIGENDIAN
    return w; // No-op on little endian hardware
#else
#  if INTVAL_SIZE == 4
    return (w << 24) | ((w & 0xff00) << 8) | ((w & 0xff0000) >> 8) | (w >> 24);
#  else
#    if INTVAL_SIZE == 8
    INTVAL r;

    r = w << 56;
    r |= (w & 0xff00) << 40;
    r |= (w & 0xff0000) << 24;
    r |= (w & 0xff000000) << 8;
    r |= (w & 0xff00000000) >> 8;
    r |= (w & 0xff0000000000) >> 24;
    r |= (w & 0xff000000000000) >> 40;
    r |= (w & 0xff00000000000000) >> 56;
    return r;
#    endif
#  endif
#endif
}
+4

( gcc), :

- : uint16_t __builtin_bswap16 (uint16_t x) x ; , 0xaabb 0xbbaa. 8 .

- : uint32_t __builtin_bswap32 (uint32_t x) __builtin_bswap16, 32 .

- : uint64_t __builtin_bswap64 (uint64_t x) __builtin_bswap32, 64 .

, , , , , gcc:)

__builtin_bswap16() __builtin_bswap32() __builtin_bswap64()

Visual Studio

unsigned short _byteswap_ushort (
   unsigned short val
);
unsigned long _byteswap_ulong (
   unsigned long val
);
unsigned __int64 _byteswap_uint64 (
   unsigned __int64 val
);

ICC _bswap16, _bswap and _bswap64 *

+3

: fooobar.com/questions/39668/...

Visual ++, : intrin.h :

16- :

unsigned short _byteswap_ushort(unsigned short value);

32- :

unsigned long _byteswap_ulong(unsigned long value);

64- :

unsigned __int64 _byteswap_uint64(unsigned __int64 value);

8- () .

, .

, , -. .

.

GCC, , :

int32_t __builtin_bswap32 (int32_t x)
int64_t __builtin_bswap64 (int64_t x)
+2

. . , C int. 16, 32, 64... , , (uint_16t, uint_32t, uint_64t),

void reverse_endianess(int* number)
{
    char byte_arr[8] = {0};
    int i;

    for (i=0; i < sizeof(int); i++) {
        byte_arr[i] = (*number & 0xFF);
        *number = *number >> 8;
    }

    *number = 0;

    for (i=0; i < sizeof(int); i++) {
        *number |=  byte_arr[i];
        *number = *number << 8;
    }
}
+1

, , , , . , .

uint32_t num = 9;
uint32_t b0,b1,b2,b3,b4,b5,b6,b7;
uint32_t res = 0;

b0 = (num & 0xf) << 28;
b1 = (num & 0xf0) << 24;
b2 = (num & 0xf00) << 20;
b3 = (num & 0xf000) << 16;
b4 = (num & 0xf0000) << 12;
b5 = (num & 0xf00000) << 8;
b6 = (num & 0xf000000) << 4;
b7 = (num & 0xf0000000) << 4;

res = b0 + b1 + b2 + b3 + b4 + b5 + b6 + b7;

printf("%d\n", res);
0

All Articles