java实现解析二进制文件

java实现解析二进制文件,第1张

/**

*

*/

package com.igen.case10

import java.io.File

import java.io.FileInputStream

import java.io.FileNotFoundException

import java.io.FileOutputStream

import java.io.IOException

import java.io.InputStream

import java.net.URISyntaxException

/**

*

* @ClassName Case10

* @Description TODO

*

* @author wjggwm

* @data 2017年2月7日 上午11:46:25

*/

public class Case10 {

static final String fileName = "/test.png"

static final String filePath = "D:/files/case10"

static final String sourceFileName = "binary"

public static void main(String[] args) {

try {

readFile(Case10.class.getResource(sourceFileName).toURI().getPath())

} catch (URISyntaxException e) {

e.printStackTrace()

}

}

/**

*

* @Description 解析二进制文件

* @param sourceFileName

*

* @author wjggwm

* @data 2017年2月7日 上午11:47:12

*/

public static void readFile(String sourceFileName) {

InputStream in = null

try {

in = new FileInputStream(sourceFileName)

// 读取字符串数据长度字节

byte[] txtLenByte = new byte[2]

in.read(txtLenByte)

int txtlen = byte2ToUnsignedShort(txtLenByte, 0)

// 读取字符串字节

byte[] txtByte = new byte[txtlen]

in.read(txtByte)

//字符串为UTF-8编码

String txt = new String(txtByte, "UTF-8")

// 输出字符串

System.out.println(txt)

// 读取图片数据长度

byte[] imgLenByte = new byte[4]

in.read(imgLenByte)

int imgLen = byte4ToInt(imgLenByte, 0)

// 读取图片数据

byte[] img = new byte[imgLen]

in.read(img)

// 生成图片文件

saveToImgByBytes(filePath, fileName, img)

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

} finally {

if (in != null) {

try {

in.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

/**

*

* @Description 将字节写入文件

* @param imgName

* @param imgByte

*

* @author wjggwm

* @data 2017年2月7日 上午11:07:45

*/

public static void saveToImgByBytes(String filePath, String imgName, byte[] imgByte) {

try {

File dic = new File(filePath)

if (!dic.exists()) {

dic.mkdirs()

}

File image = new File(filePath + imgName)

if (!image.exists()) {

image.createNewFile()

}

FileOutputStream fos = new FileOutputStream(image)

fos.write(imgByte)

fos.flush()

fos.close()

} catch (Exception e) {

e.printStackTrace()

}

}

/**

*

* @Description byte数组转换为无符号short整数

* @param bytes

* @param off

* @return

*

* @author wjggwm

* @data 2017年2月7日 上午11:05:58

*/

public static int byte2ToUnsignedShort(byte[] bytes, int off) {

// 注意高位在后面,即大小端问题

int low = bytes[off]

int high = bytes[off + 1]

return (high <<8 &0xFF00) | (low &0xFF)

}

/**

*

* @Description byte数组转换为int整数

* @param bytes

* @param off

* @return

*

* @author wjggwm

* @data 2017年2月7日 上午11:07:23

*/

public static int byte4ToInt(byte[] bytes, int off) {

// 注意高位在后面,即大小端问题

int b3 = bytes[off] &0xFF

int b2 = bytes[off + 1] &0xFF

int b1 = bytes[off + 2] &0xFF

int b0 = bytes[off + 3] &0xFF

return (b0 <<24) | (b1 <<16) | (b2 <<8) | b3

}

}

//ver:1

//resv1:0

//signature:CUC

//type:69

//no:5

//resv2:0

//seq:4

//length:56

//int2,1

//int2,2

//int1,1

//int4,30

//int1,1

//int1,7

//str,beijing

//int2,1

//int2,4

//int1,1

//int4,60

//int1,1

//int1,7

//str,tianjin

//int4,80

//二进制文件b.bin是:

//00000000h: 01 43 55 43 45 00 05 00 00 00 00 04 00 00 00 38

//00000010h: 00 01 00 02 01 00 00 00 1E 01 07 62 65 69 6A 69

//00000020h: 6E 67 00 01 00 04 01 00 00 00 3C 01 07 74 69 61

//00000030h: 6E 6A 69 6E 00 00 00 50

#pragma comment(lib,"ws2_32")

#include <stdio.h>

#include <winsock2.h>

#pragma pack(push,1)

struct _D {

    char  ver

//  char  resv1

    char  signature[3]

    char  type

    short no

    char  resv2

    int   seq

    int   length

    short int2_0

    short int2_1

    char  int1_0

    int   int4_0

    char  int1_1

    char  int1_2

    char  str_0[7]

    short int2_2

    short int2_3

    char  int1_3

    int   int4_1

    char  int1_4

    char  int1_5

    char  str_1[7]

    int   int4_2

} d

#pragma pack(pop)

FILE *f

int main() {

    f=fopen("b.bin","rb")

    if (NULL==f) {

        printf("Can not open file b.bin!\n")

        return 1

    }

    fread(&d,sizeof(struct _D),1,f)

    fclose(f)

    printf("ver:%d\n"        ,d.ver)

    printf("resv1:0\n")

    printf("signature:%.3s\n",d.signature)

    printf("type:%d\n"       ,d.type)

    printf("no:%hd\n"        ,ntohs(d.no))

    printf("resv2:%d\n"      ,d.resv2)

    printf("seq:%d\n"        ,ntohl(d.seq))

    printf("length:%d\n"     ,ntohl(d.length))

    printf("int2_0:%hd\n"    ,ntohs(d.int2_0))

    printf("int2_1:%hd\n"    ,ntohs(d.int2_1))

    printf("int1_0:%d\n"     ,d.int1_0)

    printf("int4_0:%d\n"     ,ntohl(d.int4_0))

    printf("int1_1:%d\n"     ,d.int1_1)

    printf("int1_2:%d\n"     ,d.int1_2)

    printf("str_0:%.7s\n"    ,d.str_0)

    printf("int2_2:%hd\n"    ,ntohs(d.int2_2))

    printf("int2_3:%hd\n"    ,ntohs(d.int2_3))

    printf("int1_3:%d\n"     ,d.int1_3)

    printf("int4_1:%d\n"     ,ntohl(d.int4_1))

    printf("int1_4:%d\n"     ,d.int1_4)

    printf("int1_5:%d\n"     ,d.int1_5)

    printf("str_1:%.7s\n"    ,d.str_1)

    printf("int4_2:%d\n"     ,ntohl(d.int4_2))

    return 0

}

//ver:1

//resv1:0

//signature:CUC

//type:69

//no:5

//resv2:0

//seq:4

//length:56

//int2_0:1

//int2_1:2

//int1_0:1

//int4_0:30

//int1_1:1

//int1_2:7

//str_0:beijing

//int2_2:1

//int2_3:4

//int1_3:1

//int4_1:60

//int1_4:1

//int1_5:7

//str_1:tianjin

//int4_2:80

//

代码示例


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11500819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存