您的位置:首页 > 编程语言 > Java开发

Java 中数据类型转换成 byte[] 方法

2013-09-16 13:54 417 查看
想把一个float[]转换成内存数据,查了一下,下面两个方法可以将float转成byte[]。

import java.nio.ByteBuffer;
import java.util.ArrayList;

float buffer = 0f;
ByteBuffer bbuf = ByteBuffer.allocate(4);
bbuf.putFloat(buffer);
byte[] bBuffer = bbuf.array();
bBuffer=this.dataValueRollback(bBuffer);

//数值反传
private byte[] dataValueRollback(byte[] data) {
ArrayList<Byte> al = new ArrayList<Byte>();
for (int i = data.length - 1; i >= 0; i--) {
al.add(data[i]);
}

byte[] buffer = new byte[al.size()];
for (int i = 0; i <= buffer.length - 1; i++) {
buffer[i] = al.get(i);
}
return buffer;
}


方法二

先用 Float.floatToIntBits(f)转换成int

再通过如下方法转成byte []
/**
* 将int类型的数据转换为byte数组 原理:将int数据中的四个byte取出,分别存储
*
* @param n  int数据
* @return 生成的byte数组
*/
public static byte[] intToBytes2(int n) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (n >> (24 - i * 8));
}
return b;
}

/**
* 将byte数组转换为int数据
*
* @param b 字节数组
* @return 生成的int数据
*/
public static int byteToInt2(byte[] b) {
return (((int) b[0]) << 24) + (((int) b[1]) << 16)
+ (((int) b[2]) << 8) + b[3];
}


方法三(这个是我在用的):
/**
* 浮点转换为字节
*
* @param f
* @return
*/
public static byte[] float2byte(float f) {

// 把float转换为byte[]
int fbit = Float.floatToIntBits(f);

byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (fbit >> (24 - i * 8));
}

// 翻转数组
int len = b.length;
// 建立一个与源数组元素类型相同的数组
byte[] dest = new byte[len];
// 为了防止修改源数组,将源数组拷贝一份副本
System.arraycopy(b, 0, dest, 0, len);
byte temp;
// 将顺位第i个与倒数第i个交换
for (int i = 0; i < len / 2; ++i) {
temp = dest[i];
dest[i] = dest[len - i - 1];
dest[len - i - 1] = temp;
}

return dest;

}

/**
* 字节转换为浮点
*
* @param b 字节(至少4个字节)
* @param index 开始位置
* @return
*/
public static float byte2float(byte[] b, int index) {
int l;
l = b[index + 0];
l &= 0xff;
l |= ((long) b[index + 1] << 8);
l &= 0xffff;
l |= ((long) b[index + 2] << 16);
l &= 0xffffff;
l |= ((long) b[index + 3] << 24);
return Float.intBitsToFloat(l);
}


2013-05-06 add

title Java基本类型与byte数组之间相互转换

from http://blog.sina.com.cn/s/blog_7a35101201012n0b.html
package com.my.wxf4j.utils;

import java.nio.charset.Charset;

public class ByteUtil
{
public static byte[] getBytes(short data)
{
byte[] bytes = new byte[2];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
return bytes;
}

public static byte[] getBytes(char data)
{
byte[] bytes = new byte[2];
bytes[0] = (byte) (data);
bytes[1] = (byte) (data >> 8);
return bytes;
}

public static byte[] getBytes(int data)
{
byte[] bytes = new byte[4];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
bytes[2] = (byte) ((data & 0xff0000) >> 16);
bytes[3] = (byte) ((data & 0xff000000) >> 24);
return bytes;
}

public static byte[] getBytes(long data)
{
byte[] bytes = new byte[8];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data >> 8) & 0xff);
bytes[2] = (byte) ((data >> 16) & 0xff);
bytes[3] = (byte) ((data >> 24) & 0xff);
bytes[4] = (byte) ((data >> 32) & 0xff);
bytes[5] = (byte) ((data >> 40) & 0xff);
bytes[6] = (byte) ((data >> 48) & 0xff);
bytes[7] = (byte) ((data >> 56) & 0xff);
return bytes;
}

public static byte[] getBytes(float data)
{
int intBits = Float.floatToIntBits(data);
return getBytes(intBits);
}

public static byte[] getBytes(double data)
{
long intBits = Double.doubleToLongBits(data);
return getBytes(intBits);
}

public static byte[] getBytes(String data, String charsetName)
{
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
}

public static byte[] getBytes(String data)
{
return getBytes(data, "GBK");
}

public static short getShort(byte[] bytes)
{
return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}

public static char getChar(byte[] bytes)
{
return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}

public static int getInt(byte[] bytes)
{
return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
}

public static long getLong(byte[] bytes)
{
return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))
| (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
}

public static float getFloat(byte[] bytes)
{
return Float.intBitsToFloat(getInt(bytes));
}

public static double getDouble(byte[] bytes)
{
long l = getLong(bytes);
System.out.println(l);
return Double.longBitsToDouble(l);
}

public static String getString(byte[] bytes, String charsetName)
{
return new String(bytes, Charset.forName(charsetName));
}

public static String getString(byte[] bytes)
{
return getString(bytes, "GBK");
}

public static void main(String[] args)
{
short s = 122;
int i = 122;
long l = 1222222;

char c = 'a';

float f = 122.22f;
double d = 122.22;

String string = "我是好孩子";
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(c);
System.out.println(f);
System.out.println(d);
System.out.println(string);

System.out.println("**************");

System.out.println(getShort(getBytes(s)));
System.out.println(getInt(getBytes(i)));
System.out.println(getLong(getBytes(l)));
System.out.println(getChar(getBytes(c)));
System.out.println(getFloat(getBytes(f)));
System.out.println(getDouble(getBytes(d)));
System.out.println(getString(getBytes(string)));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: