Eric Posolsky
Eric Posolsky

Reputation: 73

Problems with hex

I get some homework, but i stack in it. Maybe you can help me. Task below.

Read the keyboard integer in the decimal system and establishment of a new system of numbers.

Output in the console number written in the new notation.

I made for 2 to 10 systems only and i can't make from 10 to 36. I tried to make in second loop something like this:

if ( result > 9 ) {
    printf("%c", 55+number);
} else {
    printf("%d", result);
}

my code:

#include <stdio.h>

int main() {
    int number, base;
    int i, result;
    
    scanf("%d %d", &number, &base);
    
    if ( number < base ) {
        printf("%d\n", number);
    } else {
        for ( i = base; i <= number / base; i *= base );
        for ( int j = i; j >= base; j /= base ) {
            result = number / j;
            printf("%d", result);
            number = number % j;
        }
        printf("%d\n", number%base);
    }
    return 0;
}

Upvotes: 7

Views: 180

Answers (5)

chux
chux

Reputation: 153368

else condition needs some changes:
1. value to digit conversion needs to apply to the inner loop and to the final printf("%d\n", number % base). Instead of adding in both places, simply make your loop run to j > 0, rather than j >= base and only use the last printf() for \n.
2. Rather than using a magic number 55 use result - 10 + 'A'. It easier to understand and does not depend on ASCII - (does depend on A, B, C ... being consecutive).
3. The rest is OK.

[edit] @nos pointed out problem with if() condition.
So remove if ( number < base ) { ... else { and change for (i = base; to for (i = 1; making an even more simple solution.

  // } else {
    for (i = 1; i <= number / base; i *= base)
      ;
    int j;
    // for (j = i; j >= base; j /= base) {
    for (j = i; j > 0; j /= base) {
      result = number / j;
#if 1
      if (result > 9) {
        // printf("%c", 55 + result);
        printf("%c", result - 10 + 'A');
      } else {
        printf("%d", result);
      }
#else
      printf("%d", result);
#endif      
      number = number % j;
    }
    printf("\n");
  // }

Upvotes: 2

chux
chux

Reputation: 153368

On the other hand, should one like a solution that does not under/overflow, handles all INT_MIN to INT_MAX, no UB, only enter INT_MIN <= number <= INT_MAX, 2 <= base <= 36, and you don't mind recursion.

#include <stdio.h>
#include <stdlib.h>

void print_digit(int x, int base) {
  int quot, rem;
  quot = x/base;
  if (quot) {
    print_digit(quot, base);
  }
  rem = abs(x%base);
  if (rem > 9) {
    printf("%c", rem - 10 + 'A');
  } else {
    printf("%d", rem);
  }
}

int main() {
  int number, base;
  scanf("%d %d", &number, &base);
  if (number < 0) {
    printf("-");
  }
  print_digit(number, base);
  printf("\n");
  return 0;
}

Upvotes: 0

Bryan Ash
Bryan Ash

Reputation: 4479

You're on the right track and are very close to the answer. Your program is printing the resulting digits in two places.

I suggest making a single function to output the digits and using it in both places:

void print_digit(int number) {
  // your code here
}

Upvotes: 1

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81858

int number, base;
scanf("%d %d", &number, &base);

int divisor = base;
while (divisor <= number)
    divisor *= base;

while (divisor != 1) {
    divisor /= base;
    int digit = number / divisor;
    number -= digit * divisor;
    printf("%c", digit <= 9 ? digit + '0' : digit + 'A' - 10);
}
printf("\n");
return 0;

Caveat: Undefined behavior for numbers greater than ~200000000.

Upvotes: 1

Paul Evans
Paul Evans

Reputation: 27567

You can add characters and integers, so try something more like:

'A' + // some int value

Upvotes: 0

Related Questions