如何在java中设置八进制的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16433781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:53:58  来源:igfitidea点击:

how to set value of octal in java?

javaoctal

提问by Kapil

I am trying to write following code.but it gives me error kindly help me.

我正在尝试编写以下代码。但它给了我错误,请帮助我。

    int six=06;
    int seven=07;
    int abc=018;
    int nine=011;
    System.out.println("Octal 011 ="+nine);
    System.out.println("octal O18 =" + abc);

why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable. Why this happen? what's the reason behind this Kindly tell me.
I got Following error

为什么我不能给变量 018 和 019。我可以给变量值 020 和 021。为什么会发生这种情况?这背后的原因是什么 请告诉我。
我收到以下错误

            integer number too large: 018
            int eight=018;

采纳答案by mulya

Octal is base-8 number system, so it means digit can be from 0 to 7, you can't use digit 8 (and 9 too) in octal number system.

八进制是基数为 8 的数字系统,因此这意味着数字可以从 0 到 7,在八进制数字系统中不能使用数字 8(也不能使用数字 9)。

回答by assylias

Octal numbers (base 8) can only use the following figures: 01234567. The same way that decimal numbers (base 10) can only use 0123456789.

八进制数(基数为 8)只能使用以下数字:01234567. 与十进制数(基数为 10)只能使用0123456789.

So in octal representation, 17 + 1is 20.

所以在八进制表示中,17 + 120

回答by Jon Skeet

why i cant give 018 and 019 to variable.

为什么我不能给变量 018 和 019。

Because an integer literal prefixed with 0is treated as octal, and '8' and '9' aren't valid octal digits.

因为前缀0为的整数文字被视为八进制,并且 '8' 和 '9' 不是有效的八进制数字。

From section 3.10.1 of the JLS:

来自JLS 的第 3.10.1 节

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

八进制数字由一个 ASCII 数字 0 后跟一个或多个 ASCII 数字 0 到 7 并散布下划线组成,可以表示正整数、零或负整数。

Trying to use '8' in an octal number is like trying to use 'G' in hex... it's simple not part of the set of symbols used in that base.

尝试在八进制数中使用 '8' 就像尝试在十六进制中使用 'G' 一样......这很简单,不是该基数中使用的符号集的一部分。

回答by Hyman

Because it's octal, an octal number has 8digits which spans from 0to 7inclusive. For the same reason 12would be an invalid binary number.

因为它是八进制的,所以八进制数的8位数从07包含。出于同样的原因,12将是一个无效的二进制数。

You need at least base 9to have 18and a normal decimal base for 19.

你至少需要基地9拥有18和正常的十进制基数19

回答by Ted Hopp

When an integer literal starts with 0 in Java, it's assumed to be in octal notation. The digits 8and 9are illegal in octal—the digits can range only between 0 and 7.

当整数文字在 Java 中以 0 开头时,它被假定为八进制表示法。八进制中的数字89是非法的——数字的范围只能在 0 到 7 之间。

回答by NPE

why i cant give 018 and 019 to variable.i can give value 020 and 021 to variable.

为什么我不能给变量 018 和 019。我可以给变量值 020 和 021。

The leading zero signifies an octal literal. However, 8and 9are not valid octal digits. This makes 018and 019invalid.

前导零表示八进制文字。但是,89不是有效的八进制数字。这使得018019无效的。

回答by Owen Cao

The prefix 0 indicates octal(8 base)(digits 0-7).

前缀 0 表示八进制(8 进制)(数字 0-7)。

public class MainClass{

  public static void main(String[] argv){

    int intValue = 034;  // 28 in decimal
    int six = 06; // Equal to decimal 6
    int seven = 07; // Equal to decimal 7
    int eight = 010; // Equal to decimal 8
    int nine = 011; // Equal to decimal 9

    System.out.println("Octal 010 = " + eight);

  }

}

回答by Kanagavelu Sugumar

// Decimal declaration and possible chars are [0-9]
int decimal    =  495;        

// HexaDecimal declaration starts with 0X or 0x and possible chars are [0-9A-Fa-f]
int hexa       =  0X1EF; 

// Octal declaration starts with 0 and possible chars are [0-7] 
int octal      =  0757;  

// Binary representation starts with 0B or 0b and possible chars are [0-1]  
int binary     =  0b111101111; 

If the number is string format then you can convert it into int using the below

如果数字是字符串格式,那么您可以使用以下命令将其转换为 int

String text = "0b111101111";
int value = text.toLowerCase().startsWith("0b") ? Integer.parseInt(text.substring(2), 2)
                                  : Integer.decode(text);

回答by Arun Kumar

For your query.....you assign an invalid value to the variable....your assigned value is started with 0(zero) ..which means that you are assigning an octal value to the variable and when you assign a value higher then 7 such as 018 in your case...the value excedes the range of octal variables and hence show an error...so try entering simply 18 so it would take it as an integer rather than an octal variable data type...

对于您的查询.....您为变量分配了一个无效值....您分配的值以 0(零) ..这意味着您正在为变量分配一个八进制值,并且当您分配一个值时在您的情况下,高于 7,例如 018...该值超出了八进制变量的范围,因此显示错误...所以尝试简单地输入 18,以便将其视为整数而不是八进制变量数据类型。 .