2015-02-08 33 views
-1

這可能是一個簡單的問題,但我找不到答案。如何讀取java中的十六進制整數

我試圖讀取十六進制7B,並將其轉換爲它的十進制整數(123),或至少收到字符串7B

我的代碼:在logcat的

 byte[] content = new byte[numCharsToRead]; 

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

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

輸出:

enter image description here

回答

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);    
}