Java——进制转换的一些内容
Posted 叶不修233
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java——进制转换的一些内容相关的知识,希望对你有一定的参考价值。
Java——进制转换的一些内容
1.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();
//遍历字节数组的长度,把每个字节转成String
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.字节数组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;
//方法1:10进制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();
以上是关于Java——进制转换的一些内容的主要内容,如果未能解决你的问题,请参考以下文章