记录一下java字符串转16进制字符串,16进制字符串转java字符串,16进制字符串转byte数组,byte数组转16进制字符串 16进制转10进制等常见转换
ConvertHexStrAndStrUtils
import java.nio.charset.StandardCharsets;
/**
* 蚂蚁舞
*/
public class ConvertHexStrAndStrUtils {
private static final char[] HEXES = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String bytesToHexStr(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
StringBuilder hex = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
hex.append(HEXES[(b >> 4) & 0x0F]);
hex.append(HEXES[b & 0x0F]);
}
return hex.toString().toUpperCase();
}
public static byte[] hexStrToBytes(String hex) {
if (hex == null || hex.length() == 0) {
return null;
}
char[] hexChars = hex.toCharArray();
byte[] bytes = new byte[hexChars.length / 2]; // 如果 hex 中的字符不是偶数个, 则忽略最后一个
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
}
return bytes;
}
public static String strToHexStr(String str) {
StringBuilder sb = new StringBuilder();
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(HEXES[bit]);
bit = bs[i] & 0x0f;
sb.append(HEXES[bit]);
}
return sb.toString().trim();
}
public static String hexStrToStr(String hexStr) {
//能被16整除,肯定可以被2整除
byte[] array = new byte[hexStr.length() / 2];
try {
for (int i = 0; i < array.length; i++) {
array[i] = (byte) (0xff & Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16));
}
hexStr = new String(array, StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return hexStr;
}
public static void main(String[] args) {
System.out.println(strToHexStr("蚂蚁舞"));
System.out.println(hexStrToStr("e59388e59388e59388e59388e59388e59388"));
}
}
e89a82e89a81e8889e
哈哈哈哈哈哈
hutool工具中的HexUtil(cn.hutool.core.util.HexUtil)工具类也有,使用这个工具比新建一个java文件更方便些
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
/**
* 蚂蚁舞
*/
public class ConvertHuToolHexUtil {
public static void main(String[] args) {
String str = "蚂蚁舞";
String hexStr = "e89a82e89a81e8889e";
// 将Java字符串转换为十六进制字符串
String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);
System.out.println(hex);
// 将十六进制字符数组转换为Java字符串,默认编码UTF-8
String decodedStr = HexUtil.decodeHexStr(hexStr);
System.out.println(decodedStr);
// 将字节数组转换为十六进制字符串
//String hexstr = HexUtil.encodeHexStr(byte[] data);
// 将十六进制字符串解码为byte[]
//byte[] bytes = HexUtil.decodeHex(String hexStr);
}
}