在java中将hex little endian String转换为int

时间:2015-08-18 06:17:09

标签: java type-conversion

我有小端格式的十六进制,我想将它转换为int,几个例子:

“FC5F2E00” - > 3039228

“5E602E00” - > 3039326

String s1="FC5F2E00";
String s1="5E602E00";

    public int hexToInt(String hex){

}

1 个答案:

答案 0 :(得分:3)

您必须颠倒hex-String的顺序并将String解析为Integer。 FC5F2E00变为002E5FFC,002E5FFC变为3039228。

public static int toLittleEndian(final String hex) {
    int ret = 0;
    String hexLittleEndian = "";
    if (hex.length() % 2 != 0) return ret;
    for (int i = hex.length() - 2; i >= 0; i -= 2) {
        hexLittleEndian += hex.substring(i, i + 2);
    }
    ret = Integer.parseInt(hexLittleEndian, 16);
    return ret;
}

Input: FC5F2E00
Output: 3039228