【问题标题】:java parse binary file and convert to hex [duplicate]java解析二进制文件并转换为十六进制[重复]
【发布时间】:2016-02-23 03:51:26
【问题描述】:

请仔细阅读我的问题,然后判断是否重复

我是绿色的。如果我的描述有任何错误,请帮我解决

我想用java解析一个二进制文件。第一张图是十六进制编辑器打开的文件,你可以看到从000000 0到000000 3是ef ef ef ef

这是我的代码

String filepath = "D:\\CHR_2_20151228132500.dat.gz";
File file = new File(filepath);
FileInputStream fis = new FileInputStream(file);

GZIPInputStream gzip = new GZIPInputStream(fis);

DataInputStream din = new DataInputStream(gzip);

byte[] bytes = new byte[20];

din.read(bytes, 0, 4);

for (byte b : bytes) {
    String str  = Integer.toHexString(b);
    System.out.print(str);
}

这是我解析的结果,您可以看到每个 ef 之间有 ffffff 并附加了几个零

我想在十六进制编辑器中获取与它相同的数据。我怎样才能得到这个?

【问题讨论】:

  • 这里不要发文字图片:发文字;并且不要对未引用的文本使用引号格式。

标签: java binary hex


【解决方案1】:
String str  = Integer.toHexString(b);

字节是用 Java 签名的。你需要:

String str  = Integer.toHexString(b & 0xff);

然后确保您需要两位数:

String str  = Integer.toHexString((b & 0xff)+256).substring(1);

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 2012-03-26
    • 2012-06-26
    • 1970-01-01
    • 2017-12-30
    • 2017-03-19
    • 1970-01-01
    • 2018-05-28
    • 2013-12-20
    相关资源
    最近更新 更多