why,
System.out.println(Integer.parseInt("17", 8));
print out: 15
and,
System.out.printf("%o", 17);
print out: 21
Aren't they supposed to return octal value of 17?
Your first example converts the octal value 17 to decimal, which is 15.
Your second example converts the decimal value 17 to octal, which is 21.
If you modify your second example to use 017 (octal 17), you'll get the same result.
According to the Java API
parseInt(String s, int radix)
s - This is a string representation of decimal.
radix - This would be used to convert String s into integer.
So your Integer.parseInt("17", 8) will convert "17" to decimal using base value of 8 which is resulted 15
"%o" is use to format as Octal value so 17 will be represented as a Octal value. And result will be 21
So all the outputs that you get is correct.
System.out.printf("%o", 17);
This is used to convert an integer into its octal value
System.out.println(Integer.parseInt("17", 8));
This converts string(ocatal value) to integer.
Related
I am trying to convert a double value to Packed decimal COBOL format PIC S9(5)V9(4) COMP-3/Packed decimal COBOL format PIC S9(3)V9(4) COMP-3 format in JAVA, see below example :
Double Value : 00000.6775
Converted to Packed Decimal : ^#^FS,^#
Is this conversation possible in java?
Similar questions have been answered multiple times already on Stack overflow
For s9(5)V9(4) comp-3, 123.45 is represented in byte format as
00 12 34 50 0c
The C is the sign
Yes this is possible, the JRecord package will do it (may truncate extra digits though).
The approach is
to Convert double to a unsigned and unscaled long / BigInteger i.e 123.45 --> 1234500 (for s9(5)v9(4))
Convert the long to packed decimal
At this point there are several approaches
Repeatably Divide by 100 and use the remainder to lookup the byte value in a an array. See https://github.com/bmTas/JRecord/blob/master/Source/JRecord_Project/JRecord_Common/src/main/java/net/sf/JRecord/Types/smallBin/TypePackedDecimal9.java for an example
Divide by by 10, use the remainder && shift 4 bit operator to create the answer
Convert the number to a string, Add the sign char to the end and then **new BigInteger(UnscalledNumberAsString, 16).toByteArray() will do the conversion. See Conversion for an example
I think understand what they fundamentally do - operate on bits (flip, shift, invert, etc...).
My issue is that I don't know when I'd ever need to use them, and I don't think I fully understand bits.
I know that there are 8 bits in a byte and I know that bits are either a 0 or 1. Now here is where I start to get confused... I believe data types define combinations of bits differently. So if I declare an int, 32 bits are set aside for numbers, if I declare a char, 8 bits are set aside and the bits for that data type define a letter.
Running with that idea, I did the following basic operation in java which confuses me:
int num = 00000010;
System.out.println(num);
This prints 8 and if I define num this way:
int num = 00000100;
System.out.println(num);
This prints 64
So to practice with bitwise operations (just for the hell of it) I tried this:
int num = 00000010 << 1;
System.out.println(num);
And it prints 16 where as I thought it would shift the bits by one to the left and print 64.
What is happening here, and when would I ever need to apply this method of manipulating bits?
You are accidentally specifying an octal literal when you specify a number with a leading zero.
00000010 => 1*8^1 + 0*8^0 => 8
00000100 => 1*8^2 + 0*8^1 + 0*8^0 => 64
The JLS, Section 3.10.1, describes octal and binary literals:
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.
A binary numeral consists of the leading ASCII characters 0b or 0B
followed by one or more of the ASCII digits 0 or 1 interspersed with
underscores, and can represent a positive, zero, or negative integer.
You are bit-shifting your 8 by one to the left, effectively multiplying it by 2 to get 16. In bits:
00000100 => 00001000
(8 => 16)
Binary literals are expressed with leading 0b, e.g.:
0b000010 => 2
Integer b = Integer.valueOf("444",8);
System.out.println(b);
why b=292 I can't understand this static function
and when
b=Integer.valueOf("444",16);
System.out.println(b)
why b=1092
I appreciate your help
Thanks in advance
As usual sigh the docs are there to read them. http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf%28java.lang.String,%20int%29
Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.
This means, if you pass 16 as second argument, the number will be interpreted as a hexadecimal number, thus: 4 * 16 ^ 2 + 4 * 16 + 4 = 1092. Same for octal, only with radix 8.
You are providing the radix as octal and hexa so you are getting the output as per the radix provided:
static Integer valueOf(String s, int radix)
As per the java documentation Integer.valueOf:
Returns an Integer object holding the value extracted from the
specified String when parsed with the radix given by the second
argument. The first argument is interpreted as representing a signed
integer in the radix specified by the second argument, exactly as if
the arguments were given to the parseInt(java.lang.String, int)
method. The result is an Integer object that represents the integer
value specified by the string.
Because 444 in base 8 = 292 in base 10 and 444 in base 16 = 1092 in base 10.
"444" is the string and 16 is called as the radix, one thing to be noted is that decimal is the default base.
Now the radix is the present base of the argument in this case its 16 i.e. hex which needs to be converted to default i.e. decimal so
444(hex) to decimal is 1092.
Is there any easy way to do that without creating my own methods? I only find this:
static int parseInt(String s, int radix)
Which is told to be good only for 2, 10, 8 and 16. What about the rest?
parseInt is for converting from a string representation of a number with a particular radix, to an integer value. If you want to convert a number to a representation in a particular radix, use
Integer.toString(n, radix);
where radix can be anything from 2 to 36. After that we run out of letters.
You can use this method:
Integer.toString(i, base)
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString(int,%20int)
Integer.parseInt(String, radix) is not limited to any specific radix, and can handle anything between Character.MIN_RADIX (i.e., 2) and Character.MAX_RADIX (i.e., 36). It's perfectly safe to use in your usecase.
How is it that System.out.println(052) and System.out.println(0x2a) both print 42?
Does this have to do with binary at all?
052 is an octal (base 8) literal.
0x2a is a hexadecimal (base 16) literal.
System.out.println(0x2a) uses a hex literal (0x prefix indicates this), which has a decimal equivalent of 42.
System.out.println(052) uses an octal literal (leading 0 indicates this), which has a decimal equivalent is 42.
System.out.println will print the integers according to their decimal representation. If you want to keep hex, try System.out.printf("%x\n", 0x2a), or, for octal, "%o" in place of "%x".
052, 0x2a and 42 are all different ways of writing the (decimal, base 10) number 42:
052 is octal, or base 8
0x2a is hexadecimal, or base 16
42 is the familiar decimal, base 10
java allows you to use numbers literally in your code using any of these different formats: using a leading 0x to specify hexadecimal, or a leading zero, 0 to specify octal.
This has to do with binary inasmuch as anything that you'll do with java has to do with binary.
By the way, the binary, base 2 representation of 42 is: 101010. You can also use binary literals in java by preceding them with 0b, so 0b101010 == 0x2a.
052, starts with 0, so it's an octal number and hence 8*5+2=42
0x2a starts with 0x, so it's a hexadecimal number and hence 16*2+a = 32 + 10=42
052 is an octal literal, and is equivalent to 0 * 8^2 + 5 * 8^1 + 2 * 8^0.
0x2a is a hexadecimal literal, equivalent to 2 * 16^1 + 10 * 16^0.
5 * 8 + 2 and 2 * 16 + 10 can be seen to be equivalent, as 5 * 8 is equivalent to 4 * 8 + 8, and 8 + 2 equals 10.