二进制数据转 基本数据类型

时间:2022-01-16 17:27:33


方法一:

      如果二进制数据是有连续存放的话,那么可以使用  ByteBuffer 进行转化。 代码段如下

             如果有String的话,还有另外处理

        byte b[] = new byte[16];
int t = input.read(b);
ByteBuffer dataBuffer = ByteBuffer.wrap(b);
dataBuffer.order(ByteOrder.LITTLE_ENDIAN);

int flowNO = dataBuffer.getInt();
int falg = dataBuffer.getInt();
dataBuffer.getInt();
        //String code = ByteStrHelper.getString(dataBuffer,7);         //String name = ByteStrHelper.getString(dataBuffer,9); 
    public static String getString(ByteBuffer dataBuffer, int length)    {        String str = null;        try        {            byte[] codeBuffer = new byte[length];            dataBuffer.get(codeBuffer);            str = new String(codeBuffer, 0, length, "GBK").trim();        }        catch (UnsupportedEncodingException e)        {            e.printStackTrace();        }        return str;    }


  

方法二:

     直接转化

public class F10FileWriter
{

private static Logger logger = Logger.getLogger(F10FileWriter.class);

/**
* 每个及节点的长度
*/
public final static int STRUCT_LENGTH = 256;

/**
* 文件头长度
*/
public final static int HEAD_LENGTH = 16;

/**
* @描述: 写.f10文件
* @作者: 邓小伟
* @创建日期: 2016年1月28日 下午12:37:31
* @param filePath
* @return
*/
public static void writeF10ClolumInfo(String filePath, List<F10ColumnInfo> columnList)
{
if(columnList == null || columnList.size() < 1)
return;

int count = columnList.size();
byte []byte_array = new byte[HEAD_LENGTH + STRUCT_LENGTH * count];

RandomAccessFile raFile = null;
try
{
shortToByte(byte_array,10,(short)count); //设置头部
for(int i=0; i< count; i++) {
F10ColumnInfo f10ColumnInfo = columnList.get(i);
int offset = i * STRUCT_LENGTH + HEAD_LENGTH ;
intToByte(byte_array, offset +3, f10ColumnInfo.getDate());
intToByte(byte_array, offset +7, f10ColumnInfo.getTime());
String title = f10ColumnInfo.getTitle();
String path = f10ColumnInfo.getPath();
StringToByte(byte_array,offset + 11, title, 64);
StringToByte(byte_array,offset + 75, path, 80);
intToByte(byte_array, offset +155, f10ColumnInfo.getOffset());
intToByte(byte_array, offset +159, f10ColumnInfo.getLength());
}
if (!FileHelper.exists(filePath))
{
FileHelper.createNewFile(filePath);
}
raFile = new RandomAccessFile(filePath, "rw");
raFile.write(byte_array);

} catch (Exception e)
{
logger.error("", e);
} finally
{
try
{

if (raFile != null)
{
raFile.close();
}
} catch (Exception ex)
{
logger.error("", ex);
}
}
}


private static byte[] shortToByte(byte[] byte_array, int offset, short count)
{
int temp = count;
for(int i=offset; i < offset + 2; i++) {
byte_array[i] = new Integer(temp & 0xff).byteValue(); //将最低位保存在最低位
temp = temp >> 8; //右移8位
}
return byte_array;
}

private static byte[] intToByte(byte[] byte_array, int offset, int n)
{
byte_array[offset ] = (byte)(0xff & n);
byte_array[offset + 1] = (byte)((0xff00 & n) >> 8);
byte_array[offset + 2] = (byte)((0xff0000 & n) >> 16);
byte_array[offset + 3] = (byte)((0xff000000 & n) >> 24);
return byte_array;
}

private static byte[] StringToByte(byte[] byte_array, int offset, String str , int length)
{
if(str == null || str.length() > length)
return byte_array;
byte []by = str.getBytes();
for(int i=0, len = by.length; i < len; i ++) {
byte_array[offset + i] = by[i];
}
return byte_array;
}


/**
* 判断文件和目录是否已存在
*
* @param filePath 文件和目录完整路径
* @return tru:存在 false:不存在
*/
public static boolean exists(String filePath)
{
File file = new File(filePath);
return file.exists();
}

public static void main(String[] args)
{
//String f10Path = "F:/sh/600028.f10";
String f10Path = "F:/tt/600028.f10";
InfoFileService fileService = new InfoFileService();
List list = fileService.findInfoListByPath(f10Path);
for(int i=0 ;i<list.size(); i++) {
System.out.println(list.get(i));
}

// List<F10ColumnInfo> list = new ArrayList<F10ColumnInfo>();
// F10ColumnInfo f1 = new F10ColumnInfo();
// f1.setTime(84355); f1.setTitle("最新提示"); f1.setLength(22810); f1.setOffset(1901510);f1.setDate(20140915);
//
// F10ColumnInfo f2 = new F10ColumnInfo();
// f2.setTime(130734); f2.setTitle("公司概况"); f2.setLength(14447); f2.setOffset(2796156);f2.setDate(20140915);
// list.add(f1); list.add(f2);
//new F10FileWriter().findInfoListByPath("F:/tt/600028.f10",list);

// int count = 2;
// byte[] by = new byte[16];
//
// F10FileWriter s = new F10FileWriter();
// s.shortToByte(by,10,(short)count); //设置头部
// System.out.println((short)count);
//
// int t = InfoFileService.readShort(by, 10);
// System.out.println(t);

}
}

  二进制与 基本数据类型相互转化可以参考下面的链接

   http://cuisuqiang.iteye.com/blog/1434442