进制转换(数据存储)
2015-07-08 21:03
134 查看
class ArrDemo4 { public static void main(String[] args) { tobin(0); toba(60); tohex(-6); } //十进制转二进制 /*public static void tobin(int x) { StringBuffer sb=new StringBuffer(); while(x!=0) { sb.append(x%2); x=x/2; } System.out.println(sb.reverse()); } */ //下面转十六进制 /*public static void tohex(int x) { StringBuffer sb=new StringBuffer(); while(x!=0) { int t=x&15; if(t>9) sb.append((char)(t-10+'A')); else sb.append(t); x=x>>>4; } System.out.println(sb.reverse()); }*/ //这个可以算负数,上面的转二进制的算法不能算负数; //转二进制可以算负数的算法 /*public static void tobin(int x) { StringBuffer sb=new StringBuffer(); while(x!=0) { int t=x&1; sb.append(t); x=x>>>1; } System.out.println(sb.reverse()); }*/ //所有功能合并 public static void tobin(int num)//转二进制 { arrzh(num,1,1); } public static void toba(int num)//转八进制 { arrzh(num,3,7); } public static void tohex(int num)//转十六进制 { arrzh(num,4,15); } public static void arrzh(int num,int z,int base) { StringBuffer sb=new StringBuffer(); if(num==0) System.out.println(0); else { while(num!=0) { int t=num&base; if(t>9) sb.append((char)(t-10+'A')); else sb.append(t); num=num>>>z; } System.out.println(sb.reverse()); } // int a[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; //也可以用数组来存储数据 } }
相关文章推荐
- 《opencv入门》
- 包含min函数的栈
- 自学ios之路(2)
- List 接口以及实现类和相关类源码分析
- 用 Freemarker 生成 word 文档(包含图片)
- 无插件纯Web 3D机房,HTML5+WebGL倾力打造
- LeetCode-Pow(x, n)-解题报告
- 判断我国公民的身份证是否合法
- 《opencv2入门》感兴趣区域
- LeetCode Number of 1 Bits
- 【机器学习系列】机器学习界大牛林达华推荐的书籍
- Android总结的基本机制监控事件
- 台大机器学习基石笔记(四)——VC 维3
- 浙江工业大学简易绩点计算器c++版
- 图的宽度优先遍历--最少转机
- MAC环境下生成Apple证书教程
- 蓝牙SoC芯片之争
- 读取siftgeo格式文件的matlab程序
- C 语言里面关于字符数组的函数定义(string.h)
- [MFC]CFile类实现低级文件I/O