【Java】7.0 进制转换
【二进制转十进制】
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 进制转换的更多相关文章
- java中16进制转换10进制
java中16进制转换10进制 public static void main(String[] args) { String str = "04e1"; String myStr ...
- java中的进制转换
java中的进制转换及转换函数 转自:https://blog.csdn.net/V0218/article/details/74945203 Java的进制转换 进制转换原理 十进制 转 二进制: ...
- Swift3.0 进制转换
Swift3.0 进制转换 模块可以直接使用,写的不是很好,欢迎来喷 // Data -> HexStrings func dataToHexStringArrayWithData(data: ...
- Java练习 SDUT-1253_进制转换
进制转换 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 输入一个十进制数N,将它转换成R进制数输出. Input 输入 ...
- Java基础(进制转换-)
进制概述: 进制也就是进位计数制,是人为定义的带进位的计数方法(有不带进位的计数方法,比如原始的结绳计数法,唱票时常用的“正”字计数法,以及类似的tally mark计数). 对于任何一种进制---X ...
- java中的进制转换以及字符串类和数值类的相互转化
import java.util.*; import java.io.*; import java.math.*; import java.math.*; public class Main { pu ...
- java byte 16进制转换
整型转16进制: int devIdInt = Integer.parseInt(devId);String devIdString = Integer.toHexString(devIdInt); ...
- Java 大数任意进制转换
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = ...
- JAVA学习之进制转换练习
public static void main(String[] args) { toBin(60); toBa(60); toHex(60); } /** 十进制-->二进制 */ publi ...
随机推荐
- VOR/DME程序进近、复飞保护区的绘制
今天尝试画一个典型的VOR/DME进近程序保护区. 读图 某机场VOR/DME进近程序平面图部分如下图所示: 该程序剖面图部分如下图所示: 分析 该机场采用了偏置导航台布局(导航台在机场内), ...
- 使用 Castle 实现 AOP,以及 Autofac 集成 Castle
Castle 是 2003 年诞生于 Apache Avalon 项目,目的是为了创建一个IOC 框架.发展到现在已经有四个组件: ORM组件:ActiveRecord IOC组件:Windsor 动 ...
- Spring Cloud基础
1.网站架构演变过程 传统架构(单点应用SSM或SSH)→分布式架构(项目拆分)→SOA架构(面向服务架构)→微服务架构 2.微服务概述 2.1SOA架构 面向服务的架构(SOA)是一个组件模型,它将 ...
- spring boot的 yml和properties的对比
Spring Boot 虽然做了大量的工作来简化配置,但其配置依然是相当的复杂!支持的外部配置方式就有很多种,笔者没有去统计,也许是为了灵活使用吧. application.yml 和 appli ...
- 学习了解使用docker
学习了解使用docker docker是项目开发部署相关工具容器,本文通过官网等资料阅读学习,简单介绍一些基本使用操作: docker是什么 2.docker安装使用 连接进入docker容器 doc ...
- 【ZeyFraのJavaEE开发小知识02】MybatisPlus&ElementUI
1.关于如何获得Mybatis-Plus在插入对应为自增长主键但并未对该主键赋值的实体类之后其主键值 对应数据库中某张表并未设置主键值,但其主键为自增长类型的实体类,在使用Mybatis-Plus做i ...
- HDOJ-1029(简单dp或者排序)
Ignatius and the Princess IV hdoj-1029 这里主要是先排序,因为要找出现了一半以上的数字,所以出现的数字一定在中间 方法一: #include<iostrea ...
- POJ-3259(最短路+Bellman-Ford算法判负圈)
Wormholes POJ-3259 这题是最短路问题中判断是否存在负圈的模板题. 判断负圈的一个关键就是理解:如果在图中不存在从s可达的负圈,最短路径不会经过一个顶点两次.while循环最多执行v- ...
- C# smtp邮件发送
第一种方式发送邮件,不读取配置文件发送邮件,(本地测试可以,但是服务器上不行) /// <summary> /// 发送邮件 /// </summary> /// <pa ...
- 腾讯云发布存储一体机TStor,打通全面上云“最后一公里”
随着云计算.大数据.人工智能等技术的发展,各行各业加速数据化转型,数据容量以前所未有的速度增长,本地存储难以适应数据的指数式增长. 另一方面,公有云因其易扩展.低成本.安全稳定的特点,逐渐被企业广泛应 ...