Java——進(jìn)制轉(zhuǎn)換的一些內(nèi)容
- 1.16進(jìn)制字符串String轉(zhuǎn)字節(jié)數(shù)組byte[]
- 2.16進(jìn)制字符串String轉(zhuǎn)10進(jìn)制數(shù)字int
- 3.字節(jié)數(shù)組byte[]轉(zhuǎn)字符串String
- 4.16進(jìn)制字符串String-->byte[]-->String(使用ByteBuffer轉(zhuǎn)換)
- 5.字節(jié)數(shù)組byte[]轉(zhuǎn)字符數(shù)組char[]
- 6.字節(jié)byte轉(zhuǎn)16進(jìn)制字符串String
- 7.字節(jié)數(shù)組byte[]轉(zhuǎn)16進(jìn)制數(shù)字int
- 8.字節(jié)數(shù)組byte[]轉(zhuǎn)16進(jìn)制字符串String
1.16進(jìn)制字符串String轉(zhuǎn)字節(jié)數(shù)組byte[]
String hex = "46534E3131323130393031303030313234";
public byte[] hexToBytes(String hex){byte[] bytes = Hex.decode(hex);return bytes;
}
2.16進(jìn)制字符串String轉(zhuǎn)10進(jìn)制數(shù)字int
String hex = "FFFF";
String hex2 = "0xFFFF";
public int hexToInt(String hex){int n = Integer.parseInt(hex, 16);return n;
}
3.字節(jié)數(shù)組byte[]轉(zhuǎn)字符串String
String hex = "46534E3131323130393031303030313234";
byte[] bytes5 = Hex.decode(hex);
String str = new String(bytes5);
public String bytesToString(byte[] bytes){String str = new String(bytes5);return str;
}
4.16進(jìn)制字符串String–>byte[]–>String(使用ByteBuffer轉(zhuǎn)換)
String hex = "46534E3131323130393031303030313234";
public String hexStringToString(String hex) throws CharacterCodingException {byte[] bytes = HexUtils.fromHexString(hex);ByteBuffer source = ByteBuffer.wrap(bytes);StringBuilder sb = new StringBuilder();for (int i = 0; i < bytes.length; i++) {byte[] bytes1 = new byte[1];source.get(bytes1);ByteBuffer buffer1 = ByteBuffer.allocate(1).put(bytes1);buffer1.flip();CharsetDecoder charsetDecoder = StandardCharsets.UTF_8.newDecoder();String s = charsetDecoder.decode(buffer1).toString();sb.append(s);}return sb.toString();
}
5.字節(jié)數(shù)組byte[]轉(zhuǎn)字符數(shù)組char[]
byte[] bytes = new byte[]{35,35};
public char[] bytesToChars(byte[] bytes) {Charset charset = Charset.forName("ISO-8859-1");ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);byteBuffer.put(bytes);byteBuffer.flip();CharBuffer charBuffer = charset.decode(byteBuffer);char[] array = charBuffer.array();return array;
}
6.字節(jié)byte轉(zhuǎn)16進(jìn)制字符串String
byte b = Byte.parseByte("1");
public String byteToHexString(byte b) {String str = String.format("0x%02X", b);return str;
}
7.字節(jié)數(shù)組byte[]轉(zhuǎn)16進(jìn)制數(shù)字int
byte[] bytes = new byte[]{35,35};
public int bytesToInt(byte[] bytes) {StringBuilder stringBuilder = new StringBuilder("");for (int i = 0; i < bytes.length; i++) {int b = bytes[i] & 0xff;String str = Integer.toHexString(b);if (str.length() < 2) {stringBuilder.append(0);}stringBuilder.append(str);}return Integer.parseInt(stringBuilder.toString(),16);
}
public int bytesToInt(byte[] bytes) {ByteBuffer buffer = ByteBuffer.wrap(bytes);int value = 0;for (int i = 0; i < bytes.length; i++) {value = (value << 8) | (buffer.get() & 0xFF);}return value;
}
8.字節(jié)數(shù)組byte[]轉(zhuǎn)16進(jìn)制字符串String
byte[] bytes = new byte[]{35,35};
public String toHexString(byte[] bytes) {if (null == bytes) {return null;}StringBuilder sb = new StringBuilder(bytes.length << 1);for (byte aByte : bytes) {sb.append(hex[(aByte & 0xf0) >> 4]).append(hex[(aByte & 0x0f)]);}return sb.toString();
}