Skip to content

Instantly share code, notes, and snippets.

@ariedro
Created May 19, 2020 00:02
Show Gist options
  • Save ariedro/7f35b23d56c51d4456f651bec9a41a20 to your computer and use it in GitHub Desktop.
Save ariedro/7f35b23d56c51d4456f651bec9a41a20 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned long long int numerito;
numerito string_to_uint(char *string) {
size_t length = strlen(string);
numerito res = 0;
for (size_t i = 0; i < length; i++)
res += string[i] << 8 * i;
return res;
}
void uint_to_string(char *res, size_t size, numerito num) {
for (size_t i = 0; i < size; i++)
res[i] = (num & (0XFF << 8 * i)) >> 8 * i;
}
void print_usage() {
printf("Usage: enzo [type] [value]\n\n[type] is the type that will be "
"transformed, can be \"number\" or "
"\"string\"\n[value] value of the arg\n");
}
int main(int argc, char *argv[]) {
if (argc < 3) {
print_usage();
return 1;
}
char *type = argv[1];
char *value = argv[2];
if (!strcmp(type, "string")) {
numerito num = string_to_uint(value);
printf("%d\n", num);
} else if (!strcmp(type, "number")) {
numerito num = atoi(value);
size_t size = sizeof(num);
char string_transformed[size];
uint_to_string(string_transformed, size, num);
printf("%s\n", string_transformed);
} else {
print_usage();
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment