如何用数组把10进制整数转化为16进制的字符串输出
1个回答
展开全部
byte数组转换成16进制字符串String:
public class CommonUtil {
/**
* byte数组转换成16进制字符串
* @param src
* @return
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
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();
}
}
byte数组转换成16进制字符数组String[]:
public class CommonUtil {
/**
* byte数组转换成16进制字符数组
* @param src
* @return
*/
public static String[] bytesToHexStrings(byte[] src){
if (src == null || src.length <= 0) {
return null;
}
String[] str = new String[src.length];
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
str[i] = "0";
}
str[i] = hv;
}
return str;
}
}
public class CommonUtil {
/**
* byte数组转换成16进制字符串
* @param src
* @return
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
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();
}
}
byte数组转换成16进制字符数组String[]:
public class CommonUtil {
/**
* byte数组转换成16进制字符数组
* @param src
* @return
*/
public static String[] bytesToHexStrings(byte[] src){
if (src == null || src.length <= 0) {
return null;
}
String[] str = new String[src.length];
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
str[i] = "0";
}
str[i] = hv;
}
return str;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询