闲着没事写了个简单的十进制转二进制的算法,很简单,个人记录一下,不妥之处请指正。

public static String toBinaryString(int j) {
if (j < 0) {
throw new NumberFormatException("不支持负数");
}
double i = (double) j;
StringBuilder sb = new StringBuilder();
while (true) {
if (i % 2 == 0)
sb.append("0");
else
sb.append("1");
int result = (int) (i / 2);
if (result == 1) {
sb.append("1");
break;
}
i = (double) result;
}
char[] chars = sb.toString().toCharArray();
int len = chars.length - 1;
for (int a = 0; a <= len; a++) {
char c = chars[a];
char d = chars[len];
chars[a] = d;
chars[len] = c;
len--;
}
return new String(chars);
}

最新文章

  1. TCP的阻塞和重传机制
  2. SQL语言概述
  3. 调试WEB APP多设备浏览器
  4. Android之记住密码与自动登陆实现
  5. 用Ogre实现《天龙八部》场景中水面(TerrainLiquid)详解
  6. HTML5中的localStorage用法
  7. Java安装程序制作
  8. LINUX下mysql的大小写是否区分设置 转
  9. Program Files 与Program Files (x86)
  10. 我的Linux系统的VIMRC
  11. nodeJS之进程process对象
  12. 浅析mydumper
  13. 打开即时通讯服务器openfire的大门
  14. javascript ES6 新特性之 Promise,ES7 async / await
  15. sql server 性能调优之 当前用户请求分析 (1)
  16. dSploitzANTI渗透教程之安装zANTI工具
  17. sharepoint 2007 update sharepoint 2013 found old privillege not availabel
  18. python脚本7_打印九九乘法表
  19. HDOJ.2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)
  20. Good Bye 2015 C

热门文章

  1. [PySpark] Spark SQL on a large file
  2. mybatis低版本jsr310(LocalDateTime,LocalDate等) Joda Time支持
  3. Python 的内置函数__import__
  4. 转:获取windows凭证管理器明文密码
  5. python-Web-django-路由保护
  6. Exchange2010---反垃圾邮件配置
  7. lua程序设计(第4版)第二章习题
  8. Apache——开启个人用户主页功能
  9. JAVA实验报告及第九周总结
  10. [转帖]瀚高数据库创建uuid的方法