This question already has answers here:
Converting Hexadecimal String to Decimal Integer
                                
                                    (13个回答)
                                
                        
                                去年关闭。
            
                    
我目前正在编写一个程序,该程序读取NFC标签的ID并将其反转。我现在要完成的事情是将反向ID从十六进制转换为Dec

假设该数字的ID为“ 3bde4eac”,那么相反的结果为“ ac4edb3b”

而且我真的不知道如何正确地将HexString转换为Decimal。

这是我当前的代码:

else{
            String tagInfo = tag.toString() + "\n";

            tagInfo = "";
            byte[] tagId = tag.getId();
            for(int i=n; i<tagId.length; i++){

                tagInfo += Integer.toHexString(tagId[i] & 0xFF);

            }

            String s = tagInfo;
            StringBuilder result = new StringBuilder();
            for(int n = 0; n <=s.length()-2; n=n+2) {
                result.append(new StringBuilder(s.substring(n, n + 2)).reverse());

            }
            s = result.reverse().toString();
            Long f = Long.parseLong(s, 16);
            textViewInfo.setText(s);
        }


编辑:使用“重复链接”,我能够解决问题。

我将代码的最后一部分更改为

s = result.reverse().toString();
            Long g = hex2decimal(s);
            textViewInfo.setText(g.toString());


具有功能

public static Long hex2decimal(String s) {
    String digits = "0123456789ABCDEF";
    s = s.toUpperCase();
    long val = 0;
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        long d = digits.indexOf(c);
        val = 16*val + d;
    }
    return val;
}

最佳答案

您应该尝试将反向字符串解析为十六进制,然后将获得的int值转换为十进制。签出Integer.parseInt(strValue, 16)以解析base16 /十六进制的String和Integer.toString(intValue)

关于java - 将反向的十六进制字符串转换为十进制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51782065/

10-11 22:28
查看更多