我正在将 ISBN 值写入 UHF RFID 卡,所以我需要扫描书的条形码并接收 ISBN,然后我需要将(13 位整数)的 ISBN 转换为十六进制值以写入UHF RFID 标签。
到目前为止,我可以扫描条形码并接收 ISBN 号,但我需要一些帮助来将 ISBN 转换为十六进制值,以便用 Java 写入 UHF RFID 标签。
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
您可以使用Long.valueOf(isbnString, 16)。创建一个 toHex 方法,如果输入字符串包含,"-"
则用空字符串替换它们,然后创建并返回数字。请注意,Long.valueOf
可以抛出NumberFormatException
Eg
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"));
}