简体   繁体   English

如何在 C 中进行十进制到十六进制的转换器

[英]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.所以我是 C 的绝对初学者,我必须做一个十进制到十六进制的转换器。

So I guess I would need to make a loop that loops until the result is 0.所以我想我需要做一个循环,直到结果为 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.该数字将使用 scanf 输入,因此我无法为其定制代码。

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?那么 arrays 是我问题的答案吗? 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: 老实说,我只是使用sprintf

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; array[counter]=number%16; means that the first element in the array will be number%16 - the second will be (number/16)%16 etc. 表示数组中的第一个元素将为number%16-第二个元素将为(number / 16)%16等。

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. 您需要counter来知道数组中有多少个元素(剩余多少个元素),以便以后可以将它们向后写入。

(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.) (在这里要考虑到您有一个限制int array[50];因为,如果您的人数真的很大并且您的剩余人数超过50,会发生什么?解决方案是动态地编写此代码,但是我不认为您现在应该为此担心。)

If you want an overly complicated version.如果你想要一个过于复杂的版本。 here you go.这里是 go。 Run thing from command line:从命令行运行东西:

./toHex 255 2 ./toHex 255 2

-> output will be -> ff -> output 将是 -> 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]. 您需要在循环中增加索引并将当前值放入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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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