2011-03-16 24 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; 
} 
相关问题