Enthernet Code
Enthernet Code
DIIDevHeads IoT Integration Server
Created by Enthernet Code on 6/7/2024 in #middleware-and-os
When compiling my FreeRTOS, my functions in string.c conflict with the built-in function 'tolower'
Your code is accurate and should work as intended, but just in case, here's an improved version
#include <stdio.h>

void swapBytes(uint8_t *array, size_t length)
{
// Check if the array is NULL
if (array == NULL)
{
printf("Array pointer is NULL.\n");
return;
}

// Ensure the length is a multiple of 4
if (length % 4 != 0)
{
printf("Array length must be a multiple of 4.\n");
return;
}

for (size_t i = 0; i < length; i += 4)
{
// Swap the bytes within each group of 4
uint8_t temp0 = array[i];
uint8_t temp1 = array[i + 1];
uint8_t temp2 = array[i + 2];
uint8_t temp3 = array[i + 3];

array[i] = temp3;
array[i + 1] = temp2;
array[i + 2] = temp1;
array[i + 3] = temp0;
}
}
#include <stdio.h>

void swapBytes(uint8_t *array, size_t length)
{
// Check if the array is NULL
if (array == NULL)
{
printf("Array pointer is NULL.\n");
return;
}

// Ensure the length is a multiple of 4
if (length % 4 != 0)
{
printf("Array length must be a multiple of 4.\n");
return;
}

for (size_t i = 0; i < length; i += 4)
{
// Swap the bytes within each group of 4
uint8_t temp0 = array[i];
uint8_t temp1 = array[i + 1];
uint8_t temp2 = array[i + 2];
uint8_t temp3 = array[i + 3];

array[i] = temp3;
array[i + 1] = temp2;
array[i + 2] = temp1;
array[i + 3] = temp0;
}
}
3 replies