byte字节数组怎么转换成16进制
展开全部
方法/步骤
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 static void main(String[] args) {
byte[] src = new byte[]{ 1, 2, 3, 4 };
System.out.println( bytesToHexString(src) );
System.out.println( bytesToHexStrings(src)[2] );
}
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 static void main(String[] args) {
byte[] src = new byte[]{ 1, 2, 3, 4 };
System.out.println( bytesToHexString(src) );
System.out.println( bytesToHexStrings(src)[2] );
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询