Skip to content

Instantly share code, notes, and snippets.

@eliezio
Last active December 11, 2015 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliezio/4634336 to your computer and use it in GitHub Desktop.
Save eliezio/4634336 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static void reverseBytes (char *bytes, int length) {
int left = 0;
int right = length - 1;
while (left < right) {
char t = bytes[left];
bytes[left] = bytes[right];
bytes[right] = t;
++left;
--right;
}
}
static void test1 (void)
{
char bytes1[] = {0xea, 0x45, 0x37, 0x80, 0xb1, 0x0c, 0xb8, 0xe5, 0x65, 0x8f, 0xad, 0xd8, 0xba, 0xa7, 0xf5, 0x1b};
char reversedBytes1[] = {0x1b, 0xf5, 0xa7, 0xba, 0xd8, 0xad, 0x8f, 0x65, 0xe5, 0xb8, 0x0c, 0xb1, 0x80, 0x37, 0x45, 0xea};
reverseBytes (bytes1, sizeof(bytes1));
if (memcmp (bytes1, reversedBytes1, sizeof (bytes1)) != 0) {
fprintf (stderr, "reversal #1 failed");
exit (EXIT_FAILURE);
}
}
static void test2 (void)
{
char bytes2[] = {0xBE, 0x43, 0xE3, 0x89, 0xD7};
char reversedBytes2[] = {0xD7, 0x89, 0xE3, 0x43, 0xBE};
reverseBytes (bytes2, sizeof(bytes2));
if (memcmp (bytes2, reversedBytes2, sizeof (bytes2)) != 0) {
fprintf (stderr, "reversal #2 failed");
exit (EXIT_FAILURE);
}
}
int main (void) {
test1 ();
test2 ();
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment