byte[ ]与常用基本数据类型的转换
import java.util.Arrays;
/**
* byte[]与常用基本数据类型的转换工具类
*/
public class BitOperation {
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
/**
* 十六进制字符串转byte[]
* @param values 16进制String(一个字符2字节)
* @return
*/
public static byte[] hexStrToBytes(String values) {
if (values == null || values.trim().equals("")) {
return new byte[0];
}
byte[] result = new byte[values.length() / 2];
for (int i = 0; i < result.length; i++) {
String subStr = values.substring(i * 2, i * 2 + 2);
result[i] = (byte) Integer.parseInt(subStr, 16);
}
return result;
}
/**
* byte[]转十六进制字符串
*
* @param values 16进制String(一个字符2字节)
* @return
*/
public static String bytesToHexStr(byte[] values) {
char[] hexChars = new char[values.length * 2];
for ( int j = 0; j < values.length; j++ ) {
int v = values[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
/**
* Long型转byte[]
*
* @param values long(8字节)
* @return
*/
public static byte[] longToBytes(long values){
byte[] result = new byte[8];
int temp;
for (int i = 0; i < 8; i++){
temp = (8 - 1 - i) * 8;
result[i] = (byte) ((values >>> temp) & 0xFF);
}
return result;
}
/**
* byte[]转Long型
*
* @param values long(8字节)
* @return
*/
public static long bytesToLong(byte[] values){
long result = 0;
for (int i = 0; i < 8; i++) {
result <<= 8;
result |= (values[i] & 0xff);
}
return result;
}
/**
* int型转byte[]
*
* @param values int(4字节)
* @return
*/
public static byte[] intToBytes(int values){
byte[] result = new byte[4];
int temp;
for (int i = 0; i < 4; i++){
temp = (4 - 1 - i) * 8;
result[i] = (byte) ((values >>> temp) & 0xFF);
}
return result;
}
/**
* byte[]转int型
*
* @param values int(4字节)
* @return
*/
public static int bytesToInt(byte[] values){
int result = 0;
for (int i = 0; i < 4; i++) {
result <<= 8;
result |= (values[i] & 0xff);
}
return result;
}
/**
* short转byte[]
*
* @param values short(2字节)
* @return
*/
public static byte[] shortToByte(short values){
byte[] result = new byte[2];
result[0] = (byte) (values >>> 8 & 0xFF);
result[1] = (byte) (values & 0xFF);
return result;
}
/**
* byte[]转short
*
* @param values short(2字节)
* @return
*/
public static short bytesToShort(byte[] values){
return (short) (values[0] | (values[1] << 8));
}
/**
* char转byte[]
*
* @param values char(2字节)
* @return
*/
public static byte[] charToBytes(char values){
byte[] result = new byte[2];
result[0] = (byte) (values & 0xFF);
result[1] = (byte) ((values << 8) & 0xFF);
return result;
}
/**
* char转byte[]
*
* @param values char(2字节)
* @return
*/
public static char bytesToChar(byte[] values){
return (char) (values[0] | (values[1] << 8));
}
/**
* int整型转指定长度byte[]
*
* @param values 要转换的数据
*
* @param len 转换成byte[]后的长度
* @return
*/
public static byte[] intToBytes(int values, int len){
byte[] result = new byte[len];
int temp;
for (int i = 0; i < len; i++){
temp = (len - 1 - i) * 8;
result[i] = (byte) ((values >>> temp) & 0xFF);
}
return result;
}
/**
* byte[]转指定长度int整型
*
* @param values 要转换的数据
*
* @param len 转换成int整型后的长度,取字节数组长度即可。
* @return
*/
public static int bytesToInt(byte[] values, int len){
int result = 0;
for (int i = 0; i < len; i++) {
result <<= 8;
result |= (values[i] & 0xff);
}
return result;
}
public static void main(String[] args){
int a = 519;
System.out.println("int转byte[]=" + Arrays.toString(intToBytes(a, 3)));
System.out.println("byte[]转int=" + bytesToInt(intToBytes(a, 3), intToBytes(a, 3).length));
}
}
另外还有一个比较简单的转换方法,使用的是ByteBuffer类(涉及到一个大端小端问题)出处:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Arrays;
public class BigByteUtil {
/**
* short 转 byte[]
* 大端
* @param data
* @return
*/
public static byte[] getShortBytes(short data) {
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.putShort(data);
return buffer.array();
}
/**
* chart 转 byte[]
* 大端
* @param data
* @return
*/
public static byte[] getCharBytes(char data) {
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.putChar(data);
return buffer.array();
}
/**
* int 转 byte[]
* 大端
* @param data
* @return
*/
public static byte[] getIntBytes(int data) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.putInt(data);
return buffer.array();
}
/**
* long 转 byte[]
* 大端
* @param data
* @return
*/
public static byte[] getLongBytes(long data) {
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.putLong(data);
return buffer.array();
}
/**
* float 转 byte[]
* 大端
* @param data
* @return
*/
public static byte[] getFloatBytes(float data) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.putFloat(data);
return buffer.array();
}
/**
* double 转 byte[]
* 大端
* @param data
* @return
*/
public static byte[] getDoubleBytes(double data) {
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.putDouble(data);
return buffer.array();
}
/**
* String 转 byte[]
*
* @param data
* @param charsetName
* @return
*/
public static byte[] getStringBytes(String data, String charsetName) {
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
}
/**
* String 转 byte[]
*
* @param data
* @return
*/
public static byte[] getStringBytes(String data) {
byte[] bytes = null;
if(data != null){
bytes = data.getBytes();
}else{
bytes = new byte[0];
}
return bytes;
}
/*****************************************************************************************************************************/
/**
* byte[] 转short
* 大端
* @param bytes
* @return
*/
public static short getShort(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put(bytes);
return buffer.getShort(0);
}
/**
* byte[] 转 char
* 大端
* @param bytes
* @return
*/
public static char getChar(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put(bytes);
return buffer.getChar(0);
}
/**
* byte[] 转 int
* 大端
* @param bytes
* @return
*/
public static int getInt(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put(bytes);
return buffer.getInt(0);
}
/**
* byte[] 转 long
*
* @param bytes
* @return
*/
public static long getLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put(bytes);
return buffer.getLong(0);
}
/**
* byte[] 转 float
*
* @param bytes
* @return
*/
public static float getFloat(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put(bytes);
return buffer.getFloat(0);
}
/**
* byte[] 转 double
*
* @param bytes
* @return
*/
public static double getDouble(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.put(bytes);
return buffer.getDouble(0);
}
/**
* byte[] 转 String
*
* @param bytes
* @param charsetName
* @return
*/
public static String getString(byte[] bytes, String charsetName) {
return new String(bytes, Charset.forName(charsetName));
}
/**
* byte[] 转 String
*
* @param bytes
* @return
*/
public static String getString(byte[] bytes) {
return new String(bytes);
}
public static void main(String[] args){
String str = "0123456789abcde";
long l = 6699;
int i = 2233;
short s = Short.MAX_VALUE;
char c = 'c';
System.out.println("?转byte[]=" + Arrays.toString(getCharBytes(c)));
System.out.println("byte[]转?=" + getChar(getCharBytes(c)));
}
}
因为项目需要,对数据类型与byte[]进行互转,所以最新学习了总结了一下。以上方法测试没有问题(16进制字符串的转换有点问题),不过在此有一个问题想了一天也没想通,希望哪位大神看见了解答一下(感谢^_^):
在用以上16进制字符串转byte[ ]时,string长度为偶数时没问题,但如果string长度为奇数,则转过来的byte[ ]会少一个字节,反转也会少一个字符,因为for循环中使用的长度是string的长度,又因长度是int型,除2会直接取整,所以导致少循环了一次,截取时是截取的两个,在长度为偶数时就算少循环一次也正好可以截取完数据,所以导致在长度为偶数时没问题,长度为奇数时就会少一个字节或字符。而我百度到的转换全都是这样的,搞得我有点蒙圈。