English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Converter número em octal no Java

Para analisar e formatar um número em octal, tente o seguinte código-

Exemplo

public class Demo {
   public static void main(String args[]) {
      int val = Integer.parseInt("150", 8);
      System.out.println(val);
      String str = Integer.toString(val, 8);
      System.out.println(str);
   }
}

Resultados de Saída

104
150

No programa acima, usamos os métodos Integer.parseInt() e Integer.toString() para converter números para octal.

int val = Integer.parseInt("150", 8);
String str = Integer.toString(val, 8);

toString()O método acima representa um valor como string e formato.

Vamos definir a base como8,porque o octal usa base8Representação.