Java 如何实现各进制之间的相互转换


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
public class RadixTransformUtils {

/**
* 二进制转成16进制
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hexStr = Integer.toHexString(buf[i] & 0xFF);
if (hexStr.length() == 1) {
hexStr = '0' + hexStr;
}
sb.append(hexStr.toUpperCase());
}
log.info("二进制转成16进制后字符串:" + sb.toString());
return sb.toString();
}

/**
* 16进制字符串转二进制
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
if (hexStr.length() % 2 != 0){
hexStr = StringUtils.leftPad(hexStr,hexStr.length()+1,"0");
}
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;
}

/**
* 十进制字符串转二进制
* @param str
* @return
*/
public static String parseStr2Byte(String str) {
char[] strChar = str.toCharArray();

String result = "";

for (int i = 0; i < strChar.length; i++) {
result += Integer.toBinaryString(strChar[i]);
}
return result;
}

/**
* 二进制数组转二进制字符串
* @param b
* @return
*/
public static String parseByte2BinaryStr(byte[] b) {
if (b == null){
return "";
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
sb.append(StringUtils.leftPad(Integer.toBinaryString(b[i] & 0xFF), 8, "0"));
}
return sb.toString();
}

/**
* 二进制字符串转十六进制字符串
* @param binaryStr
* @return
*/
public static String parseBinaryStr2HexStr(String binaryStr) {
StringBuffer hexStr = new StringBuffer();
for (int i = 0 ; i < binaryStr.length() ; i = i+8) {
hexStr.append(StringUtils.leftPad(Integer.toString(Integer.parseInt(binaryStr.substring(i,i+8), 2),16), 2, "0"));;
}
return hexStr.toString();
}

/**
* 字节数不够补0
*
* @param source 原数据
* @param byteDigit 字节位数
* @return
*/
public static String Complement0(String source, int byteDigit) {
log.info("补位前数据:" + source);
if (source.length() > byteDigit * 2) {
int start = source.length() - byteDigit * 2;
source = source.substring(start);
} else {
source = StringUtils.leftPad(source, byteDigit * 2, "0");

}
log.info("补位后数据:" + source);
return source;
}

/**
* @param source 原格式数据
* @return 返回反转之后的数据
*/
public static String reversal(String source) {
log.info("反转之前数据:" + source);
char[] charArr = source.toCharArray();
List<String> array = new ArrayList<>();
for (int i = 0; i < charArr.length; i = i + 2) {
array.add(String.valueOf(charArr[i]) + String.valueOf(charArr[i + 1]));
}
Collections.reverse(array);
String resultStr = "";
for (String str : array) {
resultStr += str;
}
log.info("反转之后数据:" + resultStr);
return resultStr;
}

/**
* 字节数不够补0并反转
* @param source 原数据
* @param byteDigit 字节位数
* @return
*/
public static String Complement0Reversal(String source, int byteDigit) {
log.info("补位前数据:" + source);
source = StringUtils.leftPad(source, byteDigit * 2, "0");
log.info("补位后数据:" + source);
//返回反转之后的数据
return reversal(source);
}
}
posted @ 2022-05-24 10:50  lazy_cxy  阅读(200)  评论(0编辑  收藏  举报