java study 04day--进制转换
来源:互联网 发布:中海网络科技股份 编辑:程序博客网 时间:2024/05/02 02:12
一、十进制-》二进制
public static void toBin(int num)
{
StringBuffer sb = new StringBuffer();
while(num>0)
{
//System.out.println(num%2);
sb.append(num%2);
num = num / 2;
}
System.out.println(sb.reverse());
}
二、十进制-->十六进制
public static void toHex(int num)
{
StringBuffer sb = new StringBuffer();
for(int x=0; x<8; x++)
{
int temp = num & 15;
if(temp>9)
//System.out.println((char)(temp-10+'A'));
sb.append((char)(temp-10+'A'));
else
//System.out.println(temp);
sb.append(temp);
num = num >>> 4;
}
System.out.println(sb.reverse());
}
查表法十进制-十六进制
public static void toHex(int num)
{
char[] chs = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器。
char[] arr = new char[8];
int pos = arr.length;
while(num!=0)
{
int temp = num & 15;
//System.out.println(chs[temp]);
arr[--pos] = chs[temp];
num = num >>> 4;
}
System.out.println("pos="+pos);
//存储数据的arr数组遍历。
for(int x=pos;x<arr.length; x++)
{
System.out.print(arr[x]+",");
}
}
三、进制优化
public static void toHex(int num)
{
char[] chs = {'0','1','2','3'
,'4','5','6','7'
,'8','9','A','B'
,'C','D','E','F'};
//定义一个临时容器。
char[] arr = new char[8];
int pos = arr.length;
while(num!=0)
{
int temp = num & 15;
//System.out.println(chs[temp]);
arr[--pos] = chs[temp];
num = num >>> 4;
}
System.out.println("pos="+pos);
//存储数据的arr数组遍历。
for(int x=pos;x<arr.length; x++)
{
System.out.print(arr[x]+",");
}
}
- java study 04day--进制转换
- java study 02day-常量与变量及进制转换
- java study 04day-数组排序
- java study 04day--二维数组
- Java study 02day--类型转换与算术运算符
- java study 01day 基本常识
- java study 07day--接口
- java study 08day--多态
- java study 02day-关键字与标识符
- java study 02day--位运算符
- java study 03day--循环语句
- java study 03day--循环嵌套练习题
- java study 03day--函数及数组
- Java study 05day--面向对象
- Java study 05day-this关键字
- Java study 06day-静态代码块
- java study 07day--final关键字
- Java study 07day--抽象类
- 20120902 07
- QML编程入门
- 等待
- C++类的大小
- Qt Lighthouse QPA(Qt Platform Abstraction) 学习
- java study 04day--进制转换
- I/O读写总结
- C++ 希尔排序
- Mac OS X下重启apache
- Weblogic 9.2与Weblogic 10.3
- android控件的对齐方式详解
- jQuery UI Datepicker日期日历改造为年月日历
- Linux命令学习手册-objcopy命令
- HDU 2642 Stars