Java實(shí)現(xiàn)的進(jìn)制轉(zhuǎn)換工具類完整示例
本文實(shí)例講述了Java實(shí)現(xiàn)的進(jìn)制轉(zhuǎn)換工具類。分享給大家供大家參考,具體如下:
import java.nio.charset.Charset;
/**
* 十六進(jìn)制(簡(jiǎn)寫為hex或下標(biāo)16)在數(shù)學(xué)中是一種逢16進(jìn)1的進(jìn)位制,一般用數(shù)字0到9和字母A到F表示(其中:A~F即10~15)。<br>
* 例如十進(jìn)制數(shù)57,在二進(jìn)制寫作111001,在16進(jìn)制寫作39。<br>
* 像java,c這樣的語言為了區(qū)分十六進(jìn)制和十進(jìn)制數(shù)值,會(huì)在十六進(jìn)制數(shù)的前面加上 0x,比如0x20是十進(jìn)制的32,而不是十進(jìn)制的20<br>
*
* 參考:https://my.oschina.net/xinxingegeya/blog/287476
*
* @author Looly
*
*/
public class HexKit {
/**
* 用于建立十六進(jìn)制字符的輸出的小寫字符數(shù)組
*/
private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/**
* 用于建立十六進(jìn)制字符的輸出的大寫字符數(shù)組
*/
private static final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
//---------------------------------------------------------------------------------------------------- encode
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
*
* @param data byte[]
* @return 十六進(jìn)制char[]
*/
public static char[] encodeHex(byte[] data) {
return encodeHex(data, true);
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
*
* @param str 字符串
* @param charset 編碼
* @return 十六進(jìn)制char[]
*/
public static char[] encodeHex(String str, Charset charset) {
return encodeHex(StrKit.getBytes(str, charset), true);
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
*
* @param data byte[]
* @param toLowerCase <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式
* @return 十六進(jìn)制char[]
*/
public static char[] encodeHex(byte[] data, boolean toLowerCase) {
return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
*
* @param data byte[]
* @return 十六進(jìn)制String
*/
public static String encodeHexStr(byte[] data) {
return encodeHexStr(data, true);
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
*
* @param data byte[]
* @param toLowerCase <code>true</code> 傳換成小寫格式 , <code>false</code> 傳換成大寫格式
* @return 十六進(jìn)制String
*/
public static String encodeHexStr(byte[] data, boolean toLowerCase) {
return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
}
//---------------------------------------------------------------------------------------------------- decode
/**
* 將十六進(jìn)制字符數(shù)組轉(zhuǎn)換為字符串
*
* @param hexStr 十六進(jìn)制String
* @param charset 編碼
* @return 字符串
*/
public static String decodeHexStr(String hexStr, Charset charset) {
if(StrKit.isEmpty(hexStr)){
return hexStr;
}
return decodeHexStr(hexStr.toCharArray(), charset);
}
/**
* 將十六進(jìn)制字符數(shù)組轉(zhuǎn)換為字符串
*
* @param hexData 十六進(jìn)制char[]
* @param charset 編碼
* @return 字符串
*/
public static String decodeHexStr(char[] hexData, Charset charset) {
return StrKit.str(decodeHex(hexData), charset);
}
/**
* 將十六進(jìn)制字符數(shù)組轉(zhuǎn)換為字節(jié)數(shù)組
*
* @param hexData 十六進(jìn)制char[]
* @return byte[]
* @throws RuntimeException 如果源十六進(jìn)制字符數(shù)組是一個(gè)奇怪的長(zhǎng)度,將拋出運(yùn)行時(shí)異常
*/
public static byte[] decodeHex(char[] hexData) {
int len = hexData.length;
if ((len & 0x01) != 0) {
throw new RuntimeException("Odd number of characters.");
}
byte[] out = new byte[len >> 1];
// two characters form the hex value.
for (int i = 0, j = 0; j < len; i++) {
int f = toDigit(hexData[j], j) << 4;
j++;
f = f | toDigit(hexData[j], j);
j++;
out[i] = (byte) (f & 0xFF);
}
return out;
}
//---------------------------------------------------------------------------------------- Private method start
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符串
*
* @param data byte[]
* @param toDigits 用于控制輸出的char[]
* @return 十六進(jìn)制String
*/
private static String encodeHexStr(byte[] data, char[] toDigits) {
return new String(encodeHex(data, toDigits));
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為十六進(jìn)制字符數(shù)組
*
* @param data byte[]
* @param toDigits 用于控制輸出的char[]
* @return 十六進(jìn)制char[]
*/
private static char[] encodeHex(byte[] data, char[] toDigits) {
int l = data.length;
char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
out[j++] = toDigits[0x0F & data[i]];
}
return out;
}
/**
* 將十六進(jìn)制字符轉(zhuǎn)換成一個(gè)整數(shù)
*
* @param ch 十六進(jìn)制char
* @param index 十六進(jìn)制字符在字符數(shù)組中的位置
* @return 一個(gè)整數(shù)
* @throws RuntimeException 當(dāng)ch不是一個(gè)合法的十六進(jìn)制字符時(shí),拋出運(yùn)行時(shí)異常
*/
private static int toDigit(char ch, int index) {
int digit = Character.digit(ch, 16);
if (digit == -1) {
throw new RuntimeException("Illegal hexadecimal character " + ch + " at index " + index);
}
return digit;
}
//---------------------------------------------------------------------------------------- Private method end
/**
* 2進(jìn)制轉(zhuǎn)16進(jìn)制
* @param bString 2進(jìn)制字符串
* @return
*/
public static String binary2Hex(String bString) {
if (bString == null || bString.equals("") || bString.length() % 8 != 0)
return null;
StringBuffer tmp = new StringBuffer();
int iTmp = 0;
for (int i = 0; i < bString.length(); i += 4) {
iTmp = 0;
for (int j = 0; j < 4; j++) {
iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
}
tmp.append(Integer.toHexString(iTmp));
}
return tmp.toString();
}
/**
* 16進(jìn)制轉(zhuǎn)2進(jìn)制
* @param hexString
* @return
*/
public static String hex2Binary(String hexString) {
if (hexString == null || hexString.length() % 2 != 0)
return null;
String bString = "", tmp;
for (int i = 0; i < hexString.length(); i++) {
tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
bString += tmp.substring(tmp.length() - 4);
}
return bString;
}
/**
* 將二進(jìn)制轉(zhuǎn)換成16進(jìn)制
* @param buf
* @return
*/
public static String binary2Hex(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 將16進(jìn)制轉(zhuǎn)換為二進(jìn)制
* @param hexStr
* @return
*/
public static byte[] hex2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
PS:這里再為大家推薦幾款本站的在線進(jìn)制轉(zhuǎn)換與計(jì)算工具,相信對(duì)于大家能有所幫助:
在線任意進(jìn)制轉(zhuǎn)換工具:
http://tools.jb51.net/transcoding/hexconvert
在線標(biāo)準(zhǔn)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsq
在線科學(xué)計(jì)算器:
http://tools.jb51.net/jisuanqi/jsqkexue
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺談Spring Boot: 接口壓測(cè)及簡(jiǎn)要優(yōu)化策略
這篇文章主要介紹了淺談Spring Boot: 接口壓測(cè)及簡(jiǎn)要優(yōu)化策略,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
使用Spring Security OAuth2實(shí)現(xiàn)單點(diǎn)登錄
在本教程中,我們將討論如何使用Spring Security OAuth和Spring Boot實(shí)現(xiàn)SSO - 單點(diǎn)登錄。感興趣的朋友跟隨小編一起看看吧2019-06-06
mybatis-generator-gui根據(jù)需求改動(dòng)示例
這篇文章主要為大家介紹了mybatis-generator-gui根據(jù)需求改動(dòng)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
SpringBoot實(shí)現(xiàn)圖形驗(yàn)證碼的操作方法
隨著安全性的要求越來越高,目前許多項(xiàng)目中都使用了驗(yàn)證碼,驗(yàn)證碼也有各種類型,如 圖形驗(yàn)證碼、短信驗(yàn)證碼、郵件驗(yàn)證碼、人臉識(shí)別等,本文給大家介紹SpringBoot實(shí)現(xiàn)圖形驗(yàn)證碼的方法,感興趣的朋友跟隨小編一起看看吧2024-07-07
SpringSecurity角色權(quán)限控制(SpringBoot+SpringSecurity+JWT)
本文主要介紹了SpringSecurity角色權(quán)限控制(SpringBoot+SpringSecurity+JWT),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
ArrayList及HashMap的擴(kuò)容規(guī)則講解
今天小編就為大家分享一篇關(guān)于ArrayList及HashMap的擴(kuò)容規(guī)則講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Spring Cloud入門系列服務(wù)提供者總結(jié)
這篇文章主要介紹了Spring Cloud入門系列之服務(wù)提供者總結(jié),服務(wù)提供者使用Eureka Client組件創(chuàng)建 ,創(chuàng)建完成以后修改某文件,具體操作方法及實(shí)例代碼跟隨小編一起看看吧2021-06-06

