Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char* convert(const char* str) {
- char* end = str + strlen(str) - 1;
- char* result = malloc(sizeof(str));
- size_t result_id = 0;
- size_t current_symbol = 0;
- size_t pow_2 = 1;
- while (end >= str) {
- if (*end == '1') {
- current_symbol += pow_2;
- }
- if (pow_2 == 8 || end == str) {
- pow_2 = 1;
- if (current_symbol <= 9) {
- result[result_id++] = '0' + current_symbol;
- } else {
- result[result_id++] = 'A' + current_symbol - 10;
- }
- current_symbol = 0;
- } else {
- pow_2 *= 2;
- }
- --end;
- }
- result[result_id--] = '\0';
- for (size_t i = 0; i < result_id; ++i) {
- if (i < result_id - i) {
- char tmp = result[i];
- result[i] = result[result_id - i];
- result[result_id - i] = tmp;
- } else {
- break;
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement