如何在java中读取十六进制到整数

时间:2015-02-08 11:42:39

标签: java

这可能是一个简单的问题,但我无法找到答案。

我试图读取十六进制7B,并将其转换为十进制整数(123),或者至少接收字符串7B

我的代码:

        byte[] content = new byte[numCharsToRead];

        while ((numBytesRead = bufferedInputStream.read(content, 0,
            numCharsToRead)) != -1) {

            String temp=new String(content);
            Log.d("RS232",""+temp);

在logcat中输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下内容转换为int

 String hexDigits = "7b"
 int intValue = Integer.parseInt(hexDigits, 16); 

此外,阅读部分可以简化为:

 String temp; 
 while( (numBytesRead = bufferedInputStream.read(content, 0,
        numCharsToRead)) != -1){ 
    temp = new String(contents, 0, numBytesRead);               
 }