Endianness
This commit is contained in:
43
endianness/sol.c
Executable file
43
endianness/sol.c
Executable 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]));
|
||||
}
|
||||
Reference in New Issue
Block a user