简体   繁体   English

在java中将英特尔十六进制字符串转换为普通十六进制

[英]Convert intel hex string to normal hex in java

I have a string that is in intel hex format and it needs to be converted to hex format and then from there to decimal.我有一个intel 十六进制格式的字符串,它需要转换为十六进制格式,然后再从那里转换为十进制。 The data is sent from a gps device.数据是从 GPS 设备发送的。

This is the result I should get这是我应该得到的结果

Intel hex  : 85630800 (input)

device hex : 00086385 

decimal    : 549765 

How can I get this ?我怎样才能得到这个?

I have tried to convert intel hex to binary(as intel hex is ascii rep of binary) and binary to hex but it does not give me the value 00086385(hex) for input 85630800(intelhex)我试图将英特尔十六进制转换为二进制(因为英特尔十六进制是二进制的 ascii 表示)和二进制转换为十六进制,但它没有给我输入 85630800(intelhex) 的值 00086385(hex)

I am now looking at this library Java Intel hex parser https://github.com/j123b567/java-intelhex-parser我现在正在查看这个库 Java Intel hex parser https://github.com/j123b567/java-intelhex-parser

But this talks about converting a file in intel hex to binary format not strings.但这谈到将 intel hex 中的文件转换为二进制格式而不是字符串。 I am trying to see if I can use the code(part or full) but it seems really overkill.我想看看我是否可以使用代码(部分或全部),但它似乎真的有点矫枉过正。

Also looking at Convert Intel HEX file to binary file but it is for python .也在看Convert Intel HEX file to binary file但它是用于 python 的。

Once I get it converted to normal hex, I can use Integer.ParseInt() or Long.ParseInt() for decimal.一旦我将其转换为正常的十六进制,我就可以使用 Integer.ParseInt() 或 Long.ParseInt() 表示十进制。

But I am not able to convert the intel hex string to normal hex format.但我无法将英特尔十六进制字符串转换为正常的十六进制格式。 In there a way to convert intel hex string to normal hex string or value in java ?有没有办法将 intel 十六进制字符串转换为 Java 中的普通十六进制字符串或值?

Update更新

After reading the answers , I realize the correct question to ask was how to convert Little endian hex strings to Big endian .阅读答案后,我意识到要问的正确问题是如何将小端十六进制字符串转换为大端。 The documentation for a GPS device I had, specified the format as intel hex (maybe because Intel based processors are little endians) which threw me off from understanding the format of data I had.我拥有的 GPS 设备的文档将格式指定为intel hex (可能是因为基于 Intel 的处理器是小端),这让我无法理解我拥有的数据格式。

A capability ofInteger can help, a class one should read the javadoc of. Integer的能力可以提供帮助,一个类应该阅读 javadoc。

int input = (int) Long.parseLong("85630800", 16); // 0x85630800
int result = Integer.reverseBytes(input);

As the hex string can be larger than the signed int value, better use long.由于十六进制字符串可能大于有符号整数值,因此最好使用 long。


Addendum附录

Since java 8: parseUnsignedInt .从 java 8: parseUnsignedInt 开始

int input = Integer.parseUnsignedInt("85630000", 16);

If you need to convert a little-endian hex string into a big-endian hex string, you might start with following snippet.如果您需要将小端十六进制字符串转换为大端十六进制字符串,您可以从以下代码段开始。 Depending on what you need to solve in the big picture some tuning might be needed.根据您在大局中需要解决的问题,可能需要进行一些调整。

String s = "85630800";
StringBuilder sb = new StringBuilder(s.length());
for (int i = s.length() - 2; i >= 0; i -= 2) {
    sb.append(s.charAt(i)).append(s.charAt(i+1));
}
System.out.println(sb);

output输出

00086385

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM