【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 ...
随机推荐
- 快速读懂 HTTP/3 协议
在 深入浅出:HTTP/2 一文中详细介绍了 HTTP/2 新的特性,比如头部压缩.二进制分帧.虚拟的"流"与多路复用,性能方面比 HTTP/1 有了很大的提升.与所有性能优化过程 ...
- mysql数据库表引入redis解决方案
缓存方案 缓存方案在我的另外一篇博客里有详细说明,地址:https://www.cnblogs.com/wingfirefly/p/14419728.html 数据结构: 方案1: 1.存储结构采用h ...
- 微信小程序:post请求参数放在请求体中还是拼接到URL中需要看后台是如何接收的
前端发送post请求时,请求参数可以放在请求中,代码如下: function post(url, data, callback) { wx.request({ method: 'POST', url: ...
- hutool的DateUtil工具类
1.0.DateUitl(日期时间) 0)坐标 <dependency> <groupId>cn.hutool</groupId> <artifactId&g ...
- IDEA如何快速查看类中的属性和方法?
在idea中,当需要快速的查看一个类的所有属性和方法时,直接去代码中查看,就显得非常的麻烦,idea是有快捷键的,可显示所有的属性和方法,方法如下. 打开一个类,使用快捷键ALT+7,就可以在左侧看到 ...
- HashSet为什么可以有序输出?
首先HashSet是不保证有序,而不是保证无序,因为在HashSet中,元素是按照他们的hashCode值排序存储的.对于单个字符而言,这些hashCode就是ASCII码,因此,当按顺序添加自然数或 ...
- 关于 HTTP 后端人员需要了解的 20+ 图片!
前言 当您网上冲浪时,HTTP 协议无处不在.当您浏览网页.获取一张图片.一段视频时,HTTP 协议就正在发生. 本篇将尽可能用简短的例子和必要的说明来让您了解基础的 HTTP 知识. 目录: 什么是 ...
- 使用NATAPP内网穿透工具
准备资料 netapp客户端 百度云下载: 官网下载:https://natapp.cn/#download 按照自己的需求进行下载 可以访问到本地的web服务 下载后解压,获得natapp_wind ...
- ProBuilder快速原型开发技术 ---不规则模型与材质
ProBuilder开发模型的强大之处,还在于可以按照要求精确定制不规则模型.克隆镜像模型.给模型着色以及添加材质等,下面笔者就这几方面进行讲解. 一:定制不规则模型 PB有一个专门定制不规则模型的功 ...
- Java 多线程 01
多线程· Runnable 和 Thread 多线程的引入 * A:什么是线程 * 线程是程序执行的一条路径,一个进程中可以包含多条线程 * 多线程并发执行可以提高程序的效率,可以同时完成多项工作 * ...