Java十进制转二进制
2024-10-18 23:34:20
闲着没事写了个简单的十进制转二进制的算法,很简单,个人记录一下,不妥之处请指正。
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);
}
最新文章
- TCP的阻塞和重传机制
- SQL语言概述
- 调试WEB APP多设备浏览器
- Android之记住密码与自动登陆实现
- 用Ogre实现《天龙八部》场景中水面(TerrainLiquid)详解
- HTML5中的localStorage用法
- Java安装程序制作
- LINUX下mysql的大小写是否区分设置 转
- Program Files 与Program Files (x86)
- 我的Linux系统的VIMRC
- nodeJS之进程process对象
- 浅析mydumper
- 打开即时通讯服务器openfire的大门
- javascript ES6 新特性之 Promise,ES7 async / await
- sql server 性能调优之 当前用户请求分析 (1)
- dSploitzANTI渗透教程之安装zANTI工具
- sharepoint 2007 update sharepoint 2013 found old privillege not availabel
- python脚本7_打印九九乘法表
- HDOJ.2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)
- Good Bye 2015 C
热门文章
- [PySpark] Spark SQL on a large file
- mybatis低版本jsr310(LocalDateTime,LocalDate等) Joda Time支持
- Python 的内置函数__import__
- 转:获取windows凭证管理器明文密码
- python-Web-django-路由保护
- Exchange2010---反垃圾邮件配置
- lua程序设计(第4版)第二章习题
- Apache——开启个人用户主页功能
- JAVA实验报告及第九周总结
- [转帖]瀚高数据库创建uuid的方法