2011-03-16 62 views

回答

0
String octalNo="037"; 
System.out.println(Long.toHexString(Long.parseLong(octalNo,8))); 
3

這一切都假定你的電話號碼,之前和之後,將被存儲在一個字符串(因爲它是沒有意義的談論基地一個int /整數):

Integer.toHexString(Integer.parseInt(someOctalString, 8)); 
5

有沒有單一的方法,但你可以很容易地做到這一點通過兩個步驟:

  • 解析您的String含八進制值到int(或long,取決於預期範圍)
  • int/long設置爲十六進制的格式String

這兩個步驟可以分別使用Integer.parseInt(String, int)Integer.toString(int, int)來完成。確保使用雙參數版本,並分別將8和16分別傳遞給八進制和十六進制。

+0

如果包含其他答案包含的簡單單行代碼解決方案,我會對此答案進行投票。這個答案只是圍繞它跳舞。 –

+3

@Erick這實際上是upvote * this *的原因:它需要OP爲自己思考和嘗試。 –

0
String input = "1234"; 
String hex = Long.toHexString(Long.parseLong(input,8)); 
0
/** 
* This method takes octal input and convert it to Decimal 
* 
* @param octalInput 
* @return converted decimal value of the octal input 
*/ 
public static int ConvertOctalToDec(String octalInput) 
{ 
    int a; 
    int counter = 0; 
    double product = 0; 
    for (int index = octalInput.length() ; index > 0 ; index --) 
    { 
     a = Character.getNumericValue(octalInput.charAt(index - 1)); 
     product = product + (a * Math.pow(8 , counter)); 
     counter ++ ; 
    } 
    return (int) product; 
} 

/** 
* This methods takes octal number as input and then calls 
* ConvertOctalToDec to convert octal to decimal number then converts it 
* to Hex 
* 
* @param octalInput 
* @return Converted Hex value of octal input 
*/ 
public static String convertOctalToHex(String octalInput) 
{ 
    int decimal = ConvertOctalToDec(octalInput); 
    String hex = ""; 
    while (decimal != 0) 
    { 
     int hexValue = decimal % 16; 
     hex = convertHexToChar(hexValue) + hex; 
     decimal = decimal/16; 
    } 
    return hex; 
} 
相關問題