【问题标题】:Converting ISBN number of a Book to HexaDecimal for Writing To RFID tags将书籍的 ISBN 号转换为 HexaDecimal 以写入 RFID 标签
【发布时间】:2018-09-23 23:07:13
【问题描述】:

我正在将 ISBN 值写入 UHF RFID 卡,所以我需要扫描书的条形码并接收 ISBN,然后我需要将(13 位整数)的 ISBN 转换为十六进制值写入 UHF RFID 标签。

到目前为止,我可以扫描条形码并接收 ISBN 号,但我需要一些帮助才能将 ISBN 转换为十六进制值,以便用 Java 写入 UHF RFID 标签。

【问题讨论】:

  • 你确定这是必要的吗?似乎是一个奇怪的要求。
  • 是的,非常需要。

标签: java rfid hex isbn


【解决方案1】:
   BigInteger toHex=new BigInteger(dec,10);// use this to convert your number to big integer so that any number can be stored where dec is your input number in base 10 in string
     String s=toHex.toString(16);//convert your number into hexa string which can be directly stored in rfid tag

【讨论】:

    【解决方案2】:

    您可以使用 Long.valueOf(isbnString, 16) 。创建一个 toHex 方法,如果输入字符串包含 "-" 然后用空字符串替换它们,然后创建并返回数字。请注意Long.valueOf 可以抛出NumberFormatException 例如

    public static Long toHex(String isbn) {
        String temp = isbn;
        if (isbn.length() > 10) {
            temp = isbn.replaceAll("-", "");
        }
    
        return Long.valueOf(temp, 16);
    }
    
    public static void main(String[] args) {
            Long isbn1 = 9780071809L;
            Long isbn2 = 9780071809252L;
    
            System.out.println(toHex(isbn1.toString()));
            System.out.println(toHex(isbn2.toString()));
            System.out.println(toHex("978-0071809252"));
        }
    

    【讨论】:

    • 这不是“处理”NumberFormatException 的方法。由于它已经是RuntimeException,您应该让原始异常传播。
    • @user1099280 如果您提供的字符串无法转换为数字,例如"II" 那么方法toHex 将抛出NumberFormatException。你可以随心所欲地处理它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多