2013-08-26 180 views
0

我想將2個十六進制值相乘並得到一個十六進制值作爲輸出。我目前採取一個包含十六進制值的字符串,將其轉換爲一個整數來乘,然後將其轉換回十六進制字符串。十六進制值乘法溢出

這是一個乘以十進制(基數爲10)的示例。

//this works for all cases 
//operand1, operand2 & displayValue are strings that should be any value from 0-F 
long solutionInt = 0; 
int currentBase = 10; 
solutionInt = Integer.parseInt(operand1, currentBase) * Integer.parseInt(operand2, currentBase); 
displayValue = Integer.toString(Integer.valueOf(Long.toString(solutionInt)).intValue()); 

這是我遇到的麻煩,以十六進制乘以(16進制)

//this does NOT work for larger numbers (that would overflow) 
//operand1, operand2 & displayValue are strings that should be and value from 0-F    
long solutionInt = 0; 
int currentBase = 16;   
solutionInt = Integer.parseInt(operand1, currentBase) * Integer.parseInt(operand2, currentBase); 
displayValue = Integer.toHexString(Integer.valueOf(Long.toString(solutionInt)).intValue()); 

當較大的十六進制值相乘會導致溢出(即:0xFFFFFFFF的* 0xFFFFFFF可),我的Android應用程序崩潰,我收到以下錯誤:

FATAL EXCEPTION: main 
java.lang.IllegalStateException: Could not execute method of the activity 

我需要displayValue舉行一個十六進制字符串(如轉換爲數字)將尺寸爲int。我不確定是否有某種溢出導致這個錯誤,或者在android中有一些活動。

+0

'Integer.valueOf(Long.toString(solutionInt))。的intValue()'相當於'solutionInt'。爲什麼所有的漫長? – EJP

回答