#include #include #include "auxiliar.h" #include int check_length(char **inputs, int num_inputs, int output_length) { int input_length = strlen(inputs[0]); int selector_length = strlen(inputs[1]); int sel_combinations = 0x1 << selector_length; int num_groups = ceil(((float)input_length)/output_length); int required_combinations = 1; int i = 0; while(required_combinations < num_groups) { required_combinations = 0x1 << ++i; } if(sel_combinations < required_combinations) { return -1; } return required_combinations * output_length; } int main(int argc, char **argv) { int i; int j; if(argc != 3) { error(); } char *input = argv[1]; int output_length = str2int(argv[2]); if(!strcmp(input,"error")) { error(); } char **inputs = comma_separate(input); if(inputs == NULL) { error(); } int num_inputs = num_occurrences(input,','); if(num_inputs != 2) { error(); } int input_length = strlen(inputs[0]); int required_length = check_length(inputs,num_inputs,output_length); if(required_length == -1) { error(); } if(input_length != required_length) { expand_input(&inputs[0],required_length); } char *result = malloc(sizeof(char) * (output_length + 1)); memset(result,0x00,output_length+1); int selected = bin2dec(inputs[1]); int pos = 0; for(i = (selected+1)*(output_length)-1; i >= selected*output_length; i--) { result[pos++] = inputs[0][required_length-(i+1)]; } printf("%s",result); free(result); free_mem(inputs,num_inputs); return 0; }