How Find Given Number Is Octal or Not

How does one determine if an integer is octal or decimal when it is not entered as a string?

You can't.

0123 is a way of writing the number "eighty three" in Java.

If you wanted "one hundred and twenty three" then you have to write 123 (or 0173 or 0x7b).

You are telling your method to convert eighty three to binary, and it is doing so. There is no problem with your method.

Octal Numbers - what I might be missing to check if the input number recieved is of an Octal Format or Not?

You can use the builtin function octdect($number) from php.
An example from http://php.net/manual/en/function.octdec.php , with the same question:

<?php

function is_octal($x) {
return decoct(octdec($x)) == $x;
}

echo is_octal(077); // true
echo is_octal(195); // false

?>

how to check if number is octal and contains up to 6 digits in java

This regex checks if the number is octal, but it does not check that the number contains up to six octal digits.

In order to ensure that the number contains up to six octal digits you need to modify the regex as follows:

^0[1-7][0-7]{0,5}$

The {0,5} part says that the number of digits following the initial digit is from zero to five. Combined with the initial non-zero digit the total number will be from one to six, inclusive.

The meaning of the expression can be fully understood by following the regex Pattern reference documentation. The only tricky part is using the [1-7] part following the leading zero. It's there to ensure that numbers such as 00 do not match this regex.

how does java treat octal and decimal number

Basically if an assigned integer value begins with 0,it is considered to be an octal value and if the assigned integer value begins with 0x it is taken as hexadecimal value. Decimal system is used for programming as it is easy to comprehend. In java,

'println' outputs a single value after converting the value using the appropriate 'toString' method.By default toString() does not pass any radix value explicitly and processes value as radix of type decimal.

Refer source

As for your snippet, the following illustration will show how you arrive at the obtained result.

  1. a=10 is a decimal value. Therefore it is printed as such.
  2. b=017 is an octal value as it begins with '0'.Octal to decimal conversion takes place before the result is printed.

    CONVERSION:

    (8^0) * 7 + (8^1) * 1 = 7+8 = 15

  3. c=0X3A is a hexadecimal value as it begins with '0X'. Hexadecimal to decimal conversion takes place.

    CONVERSION:

    (16^0) * A + (16^1) * 3 = 10+48 =58 [since [A-F] in hexadecimal system indicates values [10-15]

Thus we have a=10 b=15 and c=58

NOTE: If you need to print hexa or octal value as such you can change the format specifier.

**System.out.printf("%o %x",b,c);**

OUTPUT: 3a 17

Octal Value Assigning it to int

Because in octal notation, 010 != 08. Actually, 08 doesn't exist in octal number system. All you can use are numbers 0-7 (starting with a 0).

Is 00 an integer or octal in Java?

All are integers, but...

1  is decimal
0 is decimal
01 is octal
00 is octal

From Java Language Specification (emphasis mine):

Note that octal numerals always consist of two or more digits; 0 is always considered to be a decimal numeral - not that it matters much in practice, for the numerals 0, 00, and 0x0 all represent exactly the same integer value.

Strange FormatException when converting to octal

You should know that the digits for octal numbers are in the range 0 to 7

Here, some helpful links

the octal representations of bytes range from 000 to 377?

http://www.asciitable.com/



Related Topics



Leave a reply



Submit