【二进制转十进制】

	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);
}

【Java】7.0 进制转换的更多相关文章

  1. java中16进制转换10进制

    java中16进制转换10进制 public static void main(String[] args) { String str = "04e1"; String myStr ...

  2. java中的进制转换

    java中的进制转换及转换函数 转自:https://blog.csdn.net/V0218/article/details/74945203 Java的进制转换 进制转换原理 十进制 转 二进制: ...

  3. Swift3.0 进制转换

    Swift3.0 进制转换 模块可以直接使用,写的不是很好,欢迎来喷 // Data -> HexStrings func dataToHexStringArrayWithData(data: ...

  4. Java练习 SDUT-1253_进制转换

    进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制数N,将它转换成R进制数输出. Input 输入 ...

  5. Java基础(进制转换-)

    进制概述: 进制也就是进位计数制,是人为定义的带进位的计数方法(有不带进位的计数方法,比如原始的结绳计数法,唱票时常用的“正”字计数法,以及类似的tally mark计数). 对于任何一种进制---X ...

  6. java中的进制转换以及字符串类和数值类的相互转化

    import java.util.*; import java.io.*; import java.math.*; import java.math.*; public class Main { pu ...

  7. java byte 16进制转换

    整型转16进制: int devIdInt = Integer.parseInt(devId);String devIdString = Integer.toHexString(devIdInt); ...

  8. Java 大数任意进制转换

    import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = ...

  9. JAVA学习之进制转换练习

    public static void main(String[] args) { toBin(60); toBa(60); toHex(60); } /** 十进制-->二进制 */ publi ...

随机推荐

  1. AMP & PWA

    AMP & PWA AMP is a web component framework to easily create user-first websites. stories. ads. e ...

  2. DeFi下半场,除了YFI,还有BGV!

    自今年夏季开始,DeFi市场便已经进入火热态势,且持续至今.其中,去中心化交易所(DEX)以及各种金融衍生品的出现,吸引了大批资金的进入,资本市场的目光已从传统金融移到了DeFi市场,期望着能够从De ...

  3. django学习-9.windows系统安装mysql8教程

    1.前言 mysql是最流行的关系型数据库管理系统之一,我们可以在本地windows环境下搭建一个mysql的环境,便于学习. 当前我采取的搭配是: windows7(window8和window10 ...

  4. 微信小程序:标签字符串直接变成标签来显示要通过富文本技术

    rich-text标签存在nodes属性直接接受标签字符串

  5. 5G时代,为什么NoSQL和SQL存在短板?

    01 介绍 当今的通信服务提供商(CSP)需要能够在处理海量复杂的数据的同时,不会下降或者减慢网路响应速度和可靠性.5G时代,设备和用户数量呈指数级增长,这对业务支持服务(BSS)提出了新需求,也成为 ...

  6. 基于云原生DevOps服务自动化部署前端项目学习总结

    本文主要以部署前端Vue项目为例,讲述了如何基于云原生DevOps服务自动化部署前端项目~从开发完成到线上环境,我们只需提交代码即可~ 一.引言 作为一名开发人员,日常工作中我们除了需要负责代码的开发 ...

  7. python3 获取博彩网站页面下所有域名(批量)

    已有的域名信息 详细实现过程如下 #!/usr/bin/env python # -*- coding:utf-8 -*- import requests from bs4 import Beauti ...

  8. WPF 应用 - 在 web 中启动 exe

    以下 F:/Debug/xx.exe 为客户端路径. 1. Web 调用 1.1 IE 内核的浏览器调用方式 js 函数调用如下: var a=new ActiveXObject("Wscr ...

  9. P3388 【模板】割点(割顶) 题解 (Tarjan)

    题目链接 P3388 [模板]割点(割顶) 解题思路 最近学的东西太杂了,多写点博客免得自己糊里糊涂的过去了. 这个题求割点,感觉这篇文章写得挺好. 割点是啥?如果去掉这个点之后连通图变成多个不连通图 ...

  10. [SDOI2009] HH的项链(待续)

    [SDOI2009] HH的项链(待续) 题目大意:对一个由若干个数字(可重复)组成的数列,询问\([l,r]\)中出现的不同的数字数量 考试时(考试时范围小)用的暴力,but,没有考虑数字0的情况, ...