decimal to hexadecimal,binary and octonary.
Here is a simple algorithm about 'decimal' to 'dexadecimal',and the implementation code:
/*
Convert from decimal to binary,octonary and hexadecimal.
*/
package kju.decto; import kju.print.Printer;
public class DecimalTo {
public static String decToHex(int num) {
return transDec(num, 15, 4);
} public static String decToBinary(int num) {
return transDec(num, 1, 1);
} public static String decToOctonary(int num) {
return transDec(num, 7, 3);
} /*transform decimal to others.
num: the input decimal.
base: the value to be '&'(bitwise AND) operate with 'num' for each shift operation.
offset:the number to shift each time.
*/
private static String transDec(int num, int base, int offset) {
if(num == 0)
return "0";
char[] csRef = getHexTable();
char[] csStorage = new char[32]; //store the result(32 is the max length).
int pos = csStorage.length; //position to store in the 'scStorage'.
while(num != 0) {
int temp = num & base;
csStorage[--pos] = csRef[temp];
num >>>= offset;
}
return String.valueOf(csStorage, pos, csStorage.length - pos);
} //table that contains each item of hexadecimal letter.
private static char[] getHexTable() {
return new char[]{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
} public static void main(String[] args) {
String result = decToHex(60);
Printer.println(result);
//prints:3C.
Printer.printHr();
result = decToBinary(-6);
Printer.println(result);
//prints:11111111111111111111111111111010.
Printer.printHr();
result = decToOctonary(30);
Printer.println(result);
//prints:36
}
}
Rre: Some relevant operations.
decimal to hexadecimal,binary and octonary.的更多相关文章
- Converting from Decimal Notation to Binary Notation for Fractions
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION Therefore, the conver ...
- Java开发笔记(六)特殊数字的表达
之前的文章提到,Java语言不但支持大众熟知的十进制数,也支持计算机特有的二进制数.八进制数和十六进制数.可是在给数值变量赋值的时候,等号右边的数字明显属于十进制,那究竟要如何书写其它进制的数字呢?为 ...
- ES6学习笔记(三)——数值的扩展
看到这条条目录有没有感觉很枯燥,觉得自己的工作中还用不到它所以实在没有耐心看下去,我也是最近得闲,逼自己静下心来去学习去总结,只有在别人浮躁的时候你能静下心来去学去看去总结,你才能进步.毕竟作为前端不 ...
- 史上最全NOIP初赛知识点
CSP-J/S 第一轮知识点选讲 \(NOIP\)(全国青少年信息学奥林匹克竞赛)于2019年取消.取而代之的是由\(CCF\)推出的非专业级软件能力认证,也就是现在的\(CSP-J/S\).作为一名 ...
- 史上最全的CSP-J/S 第一轮知识点
CSP-J/S 第一轮知识点选讲 \(NOIP\)(全国青少年信息学奥林匹克竞赛)于2019年取消.取而代之的是由\(CCF\)推出的非专业级软件能力认证,也就是现在的\(CSP-J/S\).作为一名 ...
- [转载]CSP-J/S 第一轮知识点选讲
CSP-J/S 第一轮知识点选讲 转载自这里 感谢原博主的大力整理! 信息学史及基本知识 一.信息学及计算机史 计算机的顶级奖项:图灵奖.冯·诺依曼奖 图灵奖:由ACM(美国计算机协会)设立于1966 ...
- http2协议翻译(转)
超文本传输协议版本 2 IETF HTTP2草案(draft-ietf-httpbis-http2-13) 摘要 本规范描述了一种优化的超文本传输协议(HTTP).HTTP/2通过引进报头字段压缩以及 ...
- java中String类型转换方法
integer to String : int i = 42;String str = Integer.toString(i);orString str = "" + i doub ...
- C++——输入、输出和文件
一.C++输入和输出概述 1.1.流和缓冲区 C++程序把输入和输出看作字节流.输入时,程序从输入流中抽取字节:输出时,程序将字节插入到输出流中.对于面相文本的程序,每个字节代表一个字符,更通俗地说, ...
随机推荐
- Solr总结
http://www.cnblogs.com/guozk/p/3498831.html Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注 ...
- 【POJ2774】Long Long Message (后缀数组)
Long Long Message Description The little cat is majoring in physics in the capital of Byterland. A p ...
- Spring与Oauth2整合示例 spring-oauth-server
原文地址:http://www.oschina.net/p/spring-oauth-server?fromerr=vpTctDBF
- java4中创建内对象的方法
在java程序中,对象可以被显式地或者隐式地创建.四种显式的创建对象的方式: ● 用new语句创建对象 ● 运用反射手段,调用java.lang.Class 或者 java.lang. ...
- qt5使用curl实现文件下载的示例程序 good
http://blog.csdn.net/xueyushenzhou/article/details/51702672#t3 http://download.csdn.net/detail/xueyu ...
- 项目经验谈---IM新消息界面刷新异常处理记录
项目中使用到ContentObserver来观察Provider的变化,当对Uri做数据库操作时对应的Provider会发一条Notify消息调用UI上层的ContentChange方法,在这个Con ...
- So many interfaces!
http://stackoverflow.com/questions/4817369/why-does-does-it-really-listt-implement-all-these-interfa ...
- 用delphi的THTTPRIO控件调用了c#写的webservice。
用delphi的THTTPRIO控件调用了c#写的webservice. 下面是我调试时遇到的一些问题: 1,导入wsdl文件:file--new----other----wenservice---W ...
- Hibernate(一)JDBC简介
在了解Hibernate之前,我们先回顾一下传统JDBC访问数据库的相关内容.重点在于分析JDBC访问存在哪些 缺陷,这些缺陷在Hibernate中是如何思考和解决的? JDBC主要对象 DriveM ...
- HDOJ/HDU 2568 前进(简单题)
Problem Description 轻松通过墓碑,进入古墓后,才发现里面别有洞天. 突然,Yifenfei发现自己周围是黑压压的一群蝙蝠,个个扇动翅膀正准备一起向他发起进攻! 形势十分危急! 好在 ...