Endianness

This commit is contained in:
Maxime Vorwerk
2024-06-24 15:29:00 +02:00
parent 43097183eb
commit bfbb5bd515
3 changed files with 177 additions and 0 deletions

43
endianness/sol.c Executable file
View File

@@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
char *find_little_endian(const char *word)
{
size_t word_len = strlen(word);
char *little_endian = (char *)malloc((2 * word_len + 1) * sizeof(char));
for (size_t i = word_len; i-- > 0;)
{
snprintf(&little_endian[(word_len - 1 - i) * 2], 3, "%02X", (unsigned char)word[i]);
}
little_endian[2 * word_len] = '\0';
return little_endian;
}
char *find_big_endian(const char *word)
{
size_t length = strlen(word);
char *big_endian = (char *)malloc((2 * length + 1) * sizeof(char));
for (size_t i = 0; i < length; i++)
{
snprintf(&big_endian[i * 2], 3, "%02X", (unsigned char)word[i]);
}
big_endian[2 * length] = '\0';
return big_endian;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
return 1;
}
puts(find_little_endian(argv[1]));;
puts(find_big_endian(argv[1]));
}