Java 十进制 向二进制 十六进制 转化
2015-07-25 23:28
597 查看
public class HeiMa { public static void main(String[] args){ int num = 26; // toConvertBinary(num); // toConverHex2(-60); toConverHex3(-60); } static void toConvertBinary(int num){ int temp; StringBuffer stringBuffer = new StringBuffer(); while(num > 0){ temp = num / 2; stringBuffer.append(num % 2); num = temp; } stringBuffer.reverse(); System.out.println(stringBuffer); } static void toConverHex(int num){ for(int i = 0; i < 8; i++){ int temp = num & 15; if(temp > 9) System.out.println((char)(temp - 10 + 'A')); else { System.out.println(temp); } num = num >> 4; } } static void toConverHex2(int num){ char []character = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B','C','D','E' }; StringBuffer stringBuffer = new StringBuffer(); for(int i = 0; i < 8; i++){ int temp = num & 15; stringBuffer.append(character[temp]); System.out.println(character[temp]); num = num >> 4; } System.out.println(stringBuffer); } static void toConverHex3(int num){ char []character = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B','C','D','E', 'F'}; StringBuffer stringBuffer = new StringBuffer(); while(num != 0){ int temp = num & 15; stringBuffer.append(character[temp]); System.out.print( "temp =" + temp ); System.out.println("..." + character[temp]); num = num >>> 4; //移动要用无符号的移位操作防止 传入值为负数 } System.out.println(stringBuffer); } }
相关文章推荐
- 第二篇Java语法总结
- JAVA学习笔记--集合
- java获取当前运行路径
- java IO图
- 在java项目开发中要加入的spring的jar包(以spring 3.1版本为例)
- Java IO(一) 之 IO总览
- Spring 与 MyBatis 集成步骤
- developerWorks® Java技术
- Java UDP 数据报
- classpath、path、JAVA_HOME的作用
- JavaSE实战——IO流
- Java线程:概念与原理
- Eclipse自动补全-重载函数
- Java HashMap LinkedHashMap 区别及原理
- Hibernate笔记①--myeclipse制动配置hibernate
- Java注解详解
- myEclipse 整合 github
- Java的构造方法
- Java 注解
- 关于jdk7的forkjoinpool