【二进制转十进制】

	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. js web简单的路由管理器

    灵感来自此博客和此库 index.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  2. 记录PyQt5 学习中遇到的一些问题

    1   信号与槽的设置中,槽函数不用写括号: btn.clicked.connect(cao()) def cao(): ******** 会报错:argument 1 has unexpected ...

  3. Java内存模型(JMM)是什么?JMM 通过控制主内存与每个线程的本地内存之间的交互,来提供内存可见性保证

    Java内存模型就是一种符合内存模型规范的,屏蔽了各种硬件和操作系统的访问差异的,保证了Java程序在各种平台下对内存的访问都能保证效果一致的机制及规范. Java内存模型是根据英文Java Memo ...

  4. 最常用SQL joins:内连接(交集)、左外连接、右外连接、左连接、右连接、全连接(并集),全外连接

    1.内连接.两个表的公共部分用Inner join,Inner join是交集的部分. Select * from TableA A inner join TableB B on A.key=B.ke ...

  5. Prism.WPF -- Prism框架使用(下)

    本文参考Prism官方示例 命令使用 Prism提供了两种命令:DelegateCommand和CompositeCommand. DelegateCommand DelegateCommand封装了 ...

  6. 缓存cache和缓冲区buffer

    一.cache 1.cache的定义.从宏观上讲,缓存是处理速度不匹配的问题.可以是静态缓存(内存缓存.磁盘缓存).动态缓存(前端的缓存)和数据库缓存.另一个角度,从CPU来看,可以是寄存器和内存之间 ...

  7. Spring-04 Bean的自动装配

    Spring-04 Bean的自动装配 Bean的自动装配 1.自动装配说明 自动装配是使用spring满足bean依赖的一种方法. spring会在应用上下文中为某个bean寻找其依赖的bean. ...

  8. 【转】理解Serverless

    理解Serverless No silver bullet. - The Mythical Man-Month 许多年前,我们开发的软件还是C/S(客户端/服务器)和MVC(模型-试图-控制器)的形式 ...

  9. MySQL:事务机制

    为什么需要事务处理? 在执行SQL语句的时候,某些业务要求,一系列操作必须全部执行,而不能仅执行一部分. MySQL5.0后引入了事务机制,MySQL支持几种基本的数据库引擎,并非所有引擎都支持事务处 ...

  10. mysql添加远程连接权限

    查看登录用户 mysql> select host,user,password from user; 想用本地IP登录,那么可以将以上的Host值改为自己的Ip即可. 这里有多个root,对应着 ...