贵阳网站建设公司排行,谷歌字体wordpress主题,做网站的财务需求,网站建设编写代码出错Java——进制转换的一些内容1.16进制字符串String转字节数组byte[]2.16进制字符串String转10进制数字int3.字节数组byte[]转字符串String4.16进制字符串String--byte[]--String#xff08;使用ByteBuffer转换#xff09;5.字节数组byte[]转字符数组char[]6.字节byte转…
Java——进制转换的一些内容1.16进制字符串String转字节数组byte[]2.16进制字符串String转10进制数字int3.字节数组byte[]转字符串String4.16进制字符串String--byte[]--String使用ByteBuffer转换5.字节数组byte[]转字符数组char[]6.字节byte转16进制字符串String7.字节数组byte[]转16进制数字int8.字节数组byte[]转16进制字符串String1.16进制字符串String转字节数组byte[]
String hex 46534E3131323130393031303030313234;
// 把16进制字符串转换为字节数组 [70, 83, 78, 49, 49, 50, 49, 48, 57, 48, 49, 48, 48, 48, 49, 50, 52]
public byte[] hexToBytes(String hex){byte[] bytes Hex.decode(hex);return bytes;
}2.16进制字符串String转10进制数字int
String hex FFFF;
String hex2 0xFFFF;
public int hexToInt(String hex){int n Integer.parseInt(hex, 16);//int n Integer.parseInt(hex2.substring(2), 16);return n;
}3.字节数组byte[]转字符串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进制字符串String–byte[]–String使用ByteBuffer转换
String hex 46534E3131323130393031303030313234;
public String hexStringToString(String hex) throws CharacterCodingException {byte[] bytes HexUtils.fromHexString(hex);// 把byte数组读取到ByteBuffer中ByteBuffer source ByteBuffer.wrap(bytes);StringBuilder sb new StringBuilder();//遍历字节数组的长度把每个字节转成Stringfor (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.字节数组byte[]转字符数组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.字节byte转16进制字符串String
byte b Byte.parseByte(1);
public String byteToHexString(byte b) {String str String.format(0x%02X, b);//需要0x...后面几位数就在最后一个x前面写几需要字母大写就把最后一个X大写//int c b 0xff;//String str Integer.toHexString(c);//不需要0x,只显示数字return str;
}7.字节数组byte[]转16进制数字int
byte[] bytes new byte[]{35,35};
//方法110进制byte转16进制字符串16进制字符串转16进制数字int
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);
}
//方法2位运算直接转数组长度必须小于等于4
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.字节数组byte[]转16进制字符串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();
}