public class TestFire {
//bytesToHexString
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return "";
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static String addRightSpaceForBytes(String str, String charset,
int length) throws UnsupportedEncodingException {
if (str == null) {
str = "";
}
byte[] strBytes = str.getBytes(charset);
int str_length = strBytes.length;
for (int i = 0; i < (length - str_length); i = i + 1) {
str = str + ' ';
}
return str;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String str=bytesToHexString(addRightSpaceForBytes("张三", "GBK", 40).getBytes("GBK"));
System.out.println(str);
}
}
关注公众号'巧叹',获取更多知识点和分布式系统项目源码及视频,300多个视频等你来拿