Java Byte 转换为 Hex 字符串:三种方法详解
要将Byte转换为hex,可以使用以下三种方法:
- 使用Java的Integer.toHexString方法:
byte b = 10;
String hex = Integer.toHexString(b & 0xFF);
System.out.println(hex);
- 使用Apache Commons Codec库的Hex类:
import org.apache.commons.codec.binary.Hex;
byte b = 10;
String hex = Hex.encodeHexString(new byte[]{b});
System.out.println(hex);
- 使用Java的String.format方法:
byte b = 10;
String hex = String.format('%02X', b);
System.out.println(hex);
以上方法中,byte b表示要转换的Byte值,hex为转换后的hex值。
原文地址: https://www.cveoy.top/t/topic/qgSh 著作权归作者所有。请勿转载和采集!