?Java?,bin2hex
??????????????????????,bin2hex
???????????????????,??????????????????????,?????????????????????:
public class Bin2HexWithNegativeNumbers { public static void main(String[] args) { byte[] signedBytes = new byte[]{-1, 2, 3, 4}; byte[] unsignedBytes = toUnsignedByteArray(signedBytes); String hexString = bytesToHex(unsignedBytes); System.out.println("Hex string: " + hexString); } private static byte[] toUnsignedByteArray(byte[] signedBytes) { byte[] unsignedBytes = new byte[signedBytes.length]; for (int i = 0; i < signedBytes.length; i++) { unsignedBytes[i] = (byte) (signedBytes[i] & 0xFF); } return unsignedBytes; } private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("X", b)); } return sb.toString(); } }
??????,??????????????signedBytes
??????????unsignedBytes
???,????bytesToHex
???????????????????????,?????????????????