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++程序把输入和输出看作字节流.输入时,程序从输入流中抽取字节:输出时,程序将字节插入到输出流中.对于面相文本的程序,每个字节代表一个字符,更通俗地说, ...
随机推荐
- Shoot the Bullet
zoj3229:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 题意:一个摄影师,在n天内给m个女神拍照.每个女神至少要 ...
- 实验一个最小的PYTHON服务器编程
没事,玩玩儿~~~:) 按书上的例子来作.. #!/usr/bin/env python #Simple Server - Chapter 1 -server.py import socket hos ...
- RH的NFS配置--简单OK
参照文档: http://wenku.baidu.com/link?url=SAcDvj8WtBd8dunC7P6FTFADYYVzzxhOiNJqbgr-aGTZovM0lHg-wbYgv9I3Lu ...
- ServletContextListener作用(转)
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享.因为Context可 ...
- 14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用
14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用 Vsftp:/data01/mysql/zjzc# ls -lt ...
- Linux 关机命令 重启命令
Linux centos重启命令: 1.reboot2.shutdown -r now 立刻重启(root用户使用)3.shutdown -r 10 过10分钟自动重启(root用户使用)4.shut ...
- 使用LoadRunner对Web Services进行调用--Add Service Call
利用LoadRunner对Web Services进行测试时,通常有三种可供采用的方法: 在LoadRunner的Web Services虚拟用户协议中,[Add Service Call] 在Loa ...
- HDOJ/HDU 2552 三足鼎立(tan()和atan()方法)
Problem Description MCA山中人才辈出,洞悉外界战火纷纷,山中各路豪杰决定出山拯救百姓于水火,曾以题数扫全场的威士忌,曾经高数九十九的天外来客,曾以一剑铸十年的亦纷菲,歃血为盟,盘 ...
- poj 3259 Wormholes【spfa判断负环】
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36729 Accepted: 13444 Descr ...
- (转)Linux中的文件描述符
本文转自:http://blog.csdn.net/cywosp/article/details/38965239 作者:cywosp 1. 概述 在Linux系统中一切皆可以看成是文件,文件又可分为 ...