十六进制转换工具
Posted guxiaohai_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了十六进制转换工具相关的知识,希望对你有一定的参考价值。
//十六进制字符串
public static final String HEXMAXSTRING = "0123456789ABCDEF";
/**
* @Author: guwenhai
* @Description: 将十六进制的字符串转换成字节数组
* @param hexString 数据消息
* @Date: 2020/11/2 18:15
*/
public static byte[] hexStrToBinaryStr(String hexString) {
if(StringUtils.isBlank(hexString)){
return null;
}
hexString = hexString.replaceAll(" ", "");
int len = hexString.length();
int index = 0;
byte[] bytes = new byte[len / 2];
while (index < len) {
if(hexString.length() >= 2){
String sub = hexString.substring(index, index + 2);
bytes[index/2] = (byte)Integer.parseInt(sub,16);
index += 2;
}
}
return bytes;
}
/**
* @Author: guwenhai
* @Description: byte[]转16进制Str
* @Date: 2020/11/2 11:58
*/
public static String ByteArrayToHexStr(byte[] byteArray){
if (byteArray == null)
return null;
char[] hexArray = HEXMAXSTRING.toCharArray();
char[] hexChars = new char[byteArray.length * 2];
for (int i = 0; i < byteArray.length; i++){
int temp = byteArray[i] & 0xFF;
hexChars[i * 2] = hexArray[temp >>> 4];
hexChars[i * 2 + 1] = hexArray[temp & 0x0F];
}
return new String(hexChars);
}
/**
* @Author: guwenhai
* @Description: 计算校验位 ,返回十六进制校验位
* @Date: 2020/11/3 20:55
*/
public static String makeCheckSum(String data) {
int dSum = 0;
int length = data.length();
int index = 0;
// 遍历十六进制,并计算总和
while (index < length) {
String s = data.substring(index, index + 2); // 截取2位字符
dSum += Integer.parseInt(s, 16); // 十六进制转成十进制 , 并计算十进制的总和
index = index + 2;
}
int mod = dSum % 256; // 用256取余,十六进制最大是FF,FF的十进制是255
String checkSumHex = Integer.toHexString(mod); // 余数转成十六进制
length = checkSumHex.length();
if (length < 2) {
checkSumHex = "0" + checkSumHex; // 校验位不足两位的,在前面补0
}
return checkSumHex;
}
/**
* @Author: guwenhai
* @Description: 字符串转换十六进制
* @param str 字符数据
* @Date: 2020/11/19 17:38
*/
public static String toHexString(String str){
if(StringUtils.isNotBlank(str)){
byte[] arrInput;
try
{
char[] chars = HEXMAXSTRING.toCharArray();
arrInput = str.getBytes("GBK");
StringBuilder sOutput = new StringBuilder(arrInput.length);
int bit;
for (int i = 0; i < arrInput.length; i++) {
bit = (arrInput[i] & 0x0f0) >> 4;
sOutput.append(chars[bit]);
bit = arrInput[i] & 0x0f;
sOutput.append(chars[bit]);
}
return sOutput.toString();
} catch (UnsupportedEncodingException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
/**
* @Author: guwenhai
* @Description: 取十六进制低位字符
* @param strhours 元数据
* @Date: 2020/11/19 19:27
*/
public static String lowOrderByte(String strhours){
String strh = "";
if(strhours.length() > 2){
strh = strhours.substring(strhours.length() -2,strhours.length()); //截取
} else if(strhours.length() == 2){
strh = strhours;
} else{
strh = "0" + strhours;
}
return strh;
}
/**
* @Author: guwenhai
* @Description: 十进制数字转换十六进制
* @param str 字符信息
* @param separator 分隔符
* @Date: 2020/11/19 18:16
*/
public static String numberHEX(String str,String separator){
if(StringUtils.isNotBlank(str)){
if(StringUtils.isNotBlank(separator)){
String[] f = str.split(separator);
String g ="";
for (int i = 0; i < f.length; i++) {
if(Integer.toHexString(Integer.valueOf(f[i])).length() < 2){
g += "0" + Integer.toHexString(Integer.valueOf(f[i]));
} else{
g += Integer.toHexString(Integer.valueOf(f[i]));
}
}
return g;
} else{
String hex = Integer.toHexString(Integer.valueOf(str));
if(hex.length() < 2){
return "0" + hex;
} else{
return hex;
}
}
}
return "";
}
/**
* @Author: guwenhai
* @Description: 二进制转换十六进制
* @param src 值
* @Date: 2020/12/2 10:43
*/
public static String parseByte2HexStr(String src) {
if(StringUtils.isNotBlank(src)){
String data = Integer.toHexString((int) Long.parseLong(src.trim(), 2));
if(data.length() < 2){
return "0" + data;
}
return data;
}
return "00";
}
/**
* @Author: guwenhai
* @Description: 16进制直接转换成为字符串(无需Unicode解码)
* @Date: 2020/12/8 9:56
*/
public static String hexStr2Str(String hexStr) {
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = HEXMAXSTRING.indexOf(hexs[2 * i]) * 16;
n += HEXMAXSTRING.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
以上是关于十六进制转换工具的主要内容,如果未能解决你的问题,请参考以下文章
Silverlight 中的十进制转换器无法正确处理丢失焦点