十进制转换为基数2-16(二进制以十六进制) [英] Decimal conversions to base numbers 2-16 (Binary through Hexadecimal)

查看:129
本文介绍了十进制转换为基数2-16(二进制以十六进制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我写一个十进制数转换成任何基本单元二进制十六进制的程序(2,3,4,...,15,16)。这是我到目前为止,从2运行任何数量 - 15导致一个无限循环。如果你为我的项目的其余任何意见,我想知道。在运行时,这会问你两个整数,首先应该是一个十进制整数> 0,第二个应该是你希望它转换为基本类型。任何及所有的意见完成,这将大大AP preciated!谢谢你。

Hey i'm writing a program that converts decimal numbers into any base unit from binary to hexadecimal (2,3,4,....,15,16). This is what I have so far, running any number from 2 - 15 results in an infinite loop. I was wondering if you had any advice for the rest of my project. When run, this will ask you for two integers, the first should be a decimal integer > 0, the second should be the base type you want it converted to. Any and all advice for completing this would be greatly appreciated! Thank you.

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

int main(void){

  int x, y, z, c;

  printf("Please enter two integers: ");
  scanf("%d", &x);
  scanf("%d", &y);

  printf("%d\n", x);
  printf("%d\n", y);
  printf(" \n");

  if(y < 2 | y > 16){
    printf("You have entered incorrect information.\n");
           return 0;
  }
  else if(y > 1 && y < 16){

       while(z != 0){
       z = (x/y);
       c = (x%y);
       printf("%d\n", z);
       }
    printf("%d\n", z);
    printf("%d\n", c);
  }
      else if(y == 16){
        printf("%X\n", x);
      }

}

**********************edit**********************

**********************edit**********************

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

int main(void){

  int x, y, z, c, i;

  printf("Please enter two integers: ");
  scanf("%d", &x);
  scanf("%d", &y);

  printf("%d\n", x);
  printf("%d\n", y);
  printf(" \n");

  if(y < 2 || y > 16){
    printf("You have entered incorrect information.\n");
           return 0;
  }
  else if(y > 1 && y < 17){

    while(x != 0){
       c = (x%y);
       x = (x/y);

       if( c > 1 && c < 10){
       printf("%d", c);

    }else if( c == 10){
        c = printf("A");
    }else if( c == 11){
        c = printf("B");
    }else if( c == 12){
        c = printf("C");
    }else if( c == 13){
        c = printf("D");
    }else if( c == 14){
        c = printf("E");
    }else if( c == 15){
        c = printf("F");
   }
  }
    printf("\n");
}

这是我到目前为止所取得的进展。我的问题是这样的:我不能得到正确的价值观A-F输出重新present号10-15。此外,我不能得到要显示的整数的正确方法。我觉得这是一个简单的办法,但我的命令不习惯呢。谢谢大家的帮助,它一直极为有利。

This is the progress i've made so far. My problems are this: I cannot get the values A-F to output correctly to represent numbers 10-15. Also I cannot get the integers to be displayed the correct way. I think this is an easy fix, but a command I'm not used to yet. Thank you all for your help, it's been extremely beneficial.

的**的 * 的**的 * 的**的 * 编辑2 的**的 * 的**的 * 的**的 * 的***

*********edit 2 ************

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

int main(void){

  int x, y, z, c; //Sets up variables to be used in program

  printf("Please enter two integers: "); //Asks for user input
  scanf("%d", &x);
  scanf("%d", &y); //stores input

  printf("%d\n", x);
  printf("%d\n", y);
  printf(" \n");

  if(y < 2 || y > 16){
    printf("You have entered incorrect information.\n");
           return 0;
  } //bug checks

  else if(y > 1 && y < 17){

    while(x != 0){
       c = (x%y);
       x = (x/y); // Loops numbers until a 0 value is reached, can be used with
                  // any Base

     if( c == 10){
        c = printf("A");
    }else if( c == 11){
        c = printf("B");
    }else if( c == 12){
        c = printf("C");
    }else if( c == 13){
        c = printf("D");
    }else if( c == 14){
        c = printf("E");
    }else if( c == 15){
        c = printf("F");
    }else{
        printf("%d", c);
    }
     // Returns for each remainer option
    }
    printf("\n");
  }
}

好吧第三次是魅力。我和我的code唯一的问题是现在,我无法弄清楚如何使它输出以相反的顺序,正确的顺序。忽略我的第二个编辑,我解决了我自己。任何投入将是真棒。我真的不知道我如何去得到它出来逆转。谢谢大家!

OKay third time is the charm. The only problem I have with my code now is that i cannot figure out how to make it output in reverse order, the proper order. Ignore my second edit, I solved that myself. Any input would be awesome. I don't really know how I'm going to get it to come out reversed. Thanks everyone!

推荐答案

由于 X 是不是在这个循环修改以Z 总是计算相同的值,循环不退出:

Since x is not modified in this loop, z is always calculated the same value, and the loop never exits:

   while(z != 0){
   z = (x/y);
   c = (x%y);
   printf("%d\n", z);
   }

要一个数字转换为不同的基地,你平时做的就是多次被基地划分数,余数给你的数字。这将打印出的数字从去年第一:

To convert a number to a difference base, what you usually do is repeatedly divide the number by the base, and the remainders give you the digits. This will print out the digits from last to first:

   while(x != 0){
      c = (x%y); /* remainder of x when divided by y */
      x = (x/y); /* x divided by y */
      printf("%d\n", c);
   }

这篇关于十进制转换为基数2-16(二进制以十六进制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆