【二进制转十进制】

	public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a binary number");
int num = Integer.parseInt(sc.nextLine()); System.out.println("Your number is: " + num);
int position = 0, sum = 0;
while(num > 0)
{
if(num%10 == 1)
{
sum = sum + (int)(Math.pow(2,position));
System.out.println("Sum: " + sum);
}
position++;
num = num / 10;
}
System.out.println("Decimal number: " + sum);
}

【二进制转十六进制】

	public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Please enter a binary number to convert to Hex: ");
int numBin = sc.nextInt();
sc.close();
int power = 0, res = 0; while(numBin > 0)
{
res = res + ((numBin%2)*(int)(Math.pow(2,power))); // extract last digit and add to red
power++; // increase the power
numBin = numBin / 10; // get rid of last digit
}
System.out.println("Num in Decimal is: " + res);
String digits = new String("0123456789ABCDEF");
String number = new String(""); int digit = 0;
while(res > 0)
{
digit = res % 16;
number = digits.charAt(digit) + number; res = res/16;
} System.out.println(number);
}

【十进制转十六进制】

	public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a decimal number to convert to hex: ");
int num = Integer.parseInt(sc.nextLine()); String digits = new String("0123456789ABCDEF");
String number = new String("");
int position = 0; while(num > 0)
{
position = num % 16;
number = digits.charAt(position) + number;
num = num / 16;
} System.out.println("Hex: " + number);
}

【十进制转二进制】

	public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number to convert to binary:");
String number = new String("");
int num = Integer.parseInt(sc.nextLine()); System.out.println("Num: " + num);
while(num > 0)
{
number = num%2 + number; num = num/2;
}
System.out.println("Binary number: " + number);
}

【十进制转十六进制】

	public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a decimal number to convert to hex:");
String number = new String(""); // string to store the hex representation
String digits = new String("0123456789ABCDEF");
int num = Integer.parseInt(sc.nextLine());
int position = 0; System.out.println("Num: " + num);
while(num > 0)
{
position = num % 16;
number = digits.charAt(position) + number; num = num/16;
}
System.out.println("Hexadecimal number: " + number);
}

最新文章

  1. GridView利用PagerTemplate做分页显示设置上一页下一页转到下拉转页
  2. opengles 矩阵计算
  3. MFC 对话框控件自动布局
  4. mysql分页原理和高效率的mysql分页查询语句
  5. Eclipse功能集合
  6. linux下history命令显示历史指令记录的使用方法
  7. 菜鸟学习WCF笔记-Address
  8. 同步推是如何给未越狱的IOS设备安装任意IPA的?
  9. Python实践:开篇
  10. SQL Server数据库学习笔记-设计表时应该考虑的因素
  11. mysql中取系统当前时间
  12. BZOJ 2243 SDOI 2011染色
  13. 时刻注意QT与Windows系统的不同(惨痛教训)
  14. 创建一个ROS包
  15. Python BeautifulSoup中文乱码问题的2种解决方法
  16. win7 64位系统装oracle11 提示环境变量path 值超过1023字符
  17. 一些重要 Docker 命令的简单介绍
  18. Batch_Size 详解
  19. 第38节:hashCode()与toString()与equals()函数的作用,内部类和匿名内部类
  20. spark基础知识

热门文章

  1. qt char与code的相互转换
  2. 智能合约稳定币USDN的价值在哪里?
  3. vue最好的ssr服务器渲染框架
  4. django学习-9.windows系统安装mysql8教程
  5. C++算法代码——求数列[coci2014/2015 contest #1]
  6. 导入Excel时,如果有多个投料信息,则循环导入
  7. ServiceMesh
  8. Docker镜像构建原理解析(不装docker也能构建镜像)
  9. docker nacos 集群部署
  10. 漏洞复现-fastjson1.2.24-RCE