简体   繁体   中英

How to make a decimal to hexadecimal converter in C

So I am an absolute beginner in C and I have to make a decimal to hexadecimal converter.

So I guess I would need to make a loop that loops until the result is 0.

But how do I make it remember all the remainders? The number is going to be input with scanf so I can't tailor the code to it.

Now I would want to do something like this

while(number(n)!=0)
{
    number0 / 16 = number1
    number0 % 16 = remainder0
    number1 / 16 = number2
    number1 % 16 = remainder1
    .....
    number(n-1) / 16 = 0
    number(n-1) % 16 = lastremainder
}

hex = lastremainder, ..., remainder2, remainder1, remainder0

But how can I make the program create variables during the loop? Do I have to use a complete different method? I took a look at other decimal to hex converters and I don't quite get how they work.

Like I said I am an absolute beginner so sorry if the question is stupid.

Thank you for the replies. So arrays are the answer to my problem? I don't fully understand them right now but thank you for the point in the right direction.

I'd simply use sprintf , to be honest:

char hex[10];
sprintf(&hex, "%x", INT_MAX);//INT_MAX macro requires limits.h, though...
printf("%s\n", hex);//prints 7fffffff

Job done...
In full, your code could look something like:

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

int main()
{
    char hex[10];
    int input;
    scanf("%d", &input);
    sprintf(&hex, "%x", input);
    printf("The number %d, turned to hexadecimal is: %s\n", input, hex);
    return 0;
}

Or even:

    int input;
    scanf("%d", &input);
    printf("The number %d, turned to hexadecimal is: %x\n", input, input);

Make an array and write the remains in it:

int array[50];
int counter=0;

[...]

while(number!=0)
{
   [...]

   array[counter]=number%16;
   number/=16;
   ++counter;
}

The line array[counter]=number%16; means that the first element in the array will be number%16 - the second will be (number/16)%16 etc.

You need the counter to know how many elements there is in the array (how much remains), so that you can later write them backwards.

(Take into consideration here that you have a limit - int array[50]; because, what happens if your number is really big and you have more than 50 remains? The solution would be to write this dynamically, but I don't think you should worry about that at this point.)

If you want an overly complicated version. here you go. Run thing from command line:

./toHex 255 2

-> output will be -> ff

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


char letterMap(float num){

  char output;
  
  switch((int) num){
  case 0:
    output = '0';
    break;
  case 1:
    output = '1';
    break;
  case 2:
    output = '2';
    break;
  case 3:
    output = '3';
    break;
  case 4:
    output = '4';
    break;
  case 5:
    output = '5';
    break;
  case 6:
    output = '6';
    break;
  case 7:
    output = '7';
    break;
  case 8:
    output = '8';
    break;
  case 9:
    output = '9';
    break;
  case 10:
    output = 'a';
    break;
  case 11:
    output = 'b';
    break;
  case 12:
    output = 'c';
    break;
  case 13:
    output = 'd';
    break;
  case 14:
    output = 'e';
    break;
  case 15:
    output = 'f';
    break; 
  }
  return output; 
}


int main(int argc, char* argv[]){


  if(argc  < 2){
    printf("Needs two arguments: number , bytes to allocate");
    return 1; 
  }

  float num = atof(argv[1]);
  float quotient = 10.0 ; 
  int bytes = atoi(argv[2]);
  printf("Starting bits: %d\n", bytes);
  printf("Num: %f\n", num); 
  char output[bytes];
  output[bytes - 1] = '\0'; 
  int index = bytes;
  
  while((num > 0) && (index >= 0)){
    printf("num: %f\n", num);
    printf("index: %d\n\n", index); 
    quotient =((int) num) % 16;
    char next = letterMap(quotient);
    printf("Next: %c\n", next); 
    output[index] = next; 

    num -= quotient;
    num /= 16;
    index--; 
  }
  char *ptr = &output[0];

  printf("Hexadecimal: %s\n",ptr ); ``
  return 0; 
}

If you want to be able to get size unlimited number, you need to use dynamic array. firstly you get the input-length by loop, and then allocate array as needed. you need to increase an index in your loop and put the current value in array[index].

Just try this:

    char hex = [], finalHex = [];
while(num != 0) {
num = num/16;
rem = num%16;
switch(rem) {
case 0 : 
hex = '0';
break;
case 1 : 
hex = '1';
break;
case 2 : 
hex = '2';
break;
case 3 : 
hex = '3';
break;
case 4 : 
hex = '4';
break;
case 5 : 
hex = '5';
break;
case 6 : 
hex = '6';
break;
case 7 : 
hex = '7';
break;
case 8 : 
hex = '8';
break;
case 9 : 
hex = '9';
break;
case 10 : 
hex = 'A';
break;
case 11 : 
hex = 'B';
break;
case 12 : 
hex = 'C';
break;
case 13 : 
hex = 'D';
break;
case 14 : 
hex = 'E';
break;
case 15 : 
hex = 'F';
break;
}
strcat(hex, finalHex);
finalHex = hex;
}
 printf("%s", finalHex);
// decimal to hex converter
char* dec2hex(int num){
  int rem = 0,i=0;
  char hex[3];
  while(num > 0 && i >=0){
    rem = num%16;
    hex[i] = rem<10 ? (char)rem+48 : (char)rem+55;
    num/=16;
    i++;
  }
  hex[i]='\0';
  return strrev(hex);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM