Why is Binary represented as a String? - java

So I am quite new to Java and motivated to learn. This question might seem simple but I genuinely don't understand and have searched Google for answers (no luck).
I am converting a decimal into Binary in Java. However, I thought representations of numbers are supposed to be given in data types int, double, and etc.
The code is as follows :
int decimal = 99;
String binary = Integer.toBinaryString(decimal);
System.out.println(binary);
Why is it String binary, should it not be any of the numerical data types?

Internally, all values are stored as binary values. Because it's easier to read, integers are converted into digits for display. Displaying a value as a binary is thus purely a representation issue.
So 99 is internally stored as 01100011. You can display it as a hexadecimal (0x63), a decimal (99), or a binary. But because the numerical value is the same in each case, the only difference is the symbols used to display it, and this symbolic representation is as a String.

The following ints are all equal:
int i = 99;
int j = 0o143;
int k = 0x63;
int l = 0b1100011;
If you want to print them, you have to convert them to a String using a utility method of Integer, or you use number formatting.

Related

How to convert a big decimal number in a String into Hexadecimal in Java?

i have 12 digit number in a string, I need to convert it to hexadecimal.
unit_address = '0064627520128';
I tried using
Integer.toHexString("string")
but it doesn't work as the decimal value is big.
Only integer values are possible to convert to hex using this but not in my case.
You could use BigInteger instead of BigDecimal. This way you could use
new BigInteger("18446744073709551616").toString(16)
If you can't change type of original object convert it to BigInteger later in method
new BigDecimal("18446744073709551616").toBigInteger().toString(16);

Converting String to BigDecimal with leading zeros

I am trying to convert an inputstream to objects and am having trouble with converting the below string to BigDecimal. I get 87.00 as the 0's are ignored. I am experimenting with DecimalFormat. Any help is appreciated. Thanks!
E.g. 0087 has to be converted to 00.87
You seem to indicate in comments that the initial string, "0087", is a fixed-point representation with two decimal places, there therefore being an implicit decimal point before the last two digits. There are several ways that you could convert that to a corresponding BigDecimal, but myself, I would go with this:
BigDecimal result = BigDecimal.valueOf(Long.parseLong("0087"), 2);
The key here is to understand that BigDecimals have two characteristic properties: an arbitrary-length sequence of decimal digits, and a scale conveying the place value of the least-significant digit, in the form of the number of digits to the right of the decimal point. The particular factory method demonstrated above accepts those as separate arguments, though it only works if the digit string is short enough to be represented as a long. The 2 corresponds directly to the number of (implicitly) fractional digits in your input.
Outputting the resulting BigDecimal as "00.87" instead of "0.87" is a separate issue, but doable, if it's really something you want.
Try this code
String str="0087";
int pos=str.lastIndexOf("0");
String resultat=str.substring(0,pos+1);
resultat+=".";
resultat+=str.substring(pos+1);
System.out.println(resultat);
new BigDecimal(resultat);

Why does byte representation in Java store data in decimal?

I was debugging a problem where I created a byte array from binary representation of strings such as below.
While debugging the array just to see how it is stored, I could see them stored internally as decimals. Why is that?
When it gets interpreted as bytecode I am assuming it will get converted as binary. Then why not store in binary in the first place.
String[] binArray = {"10101","11100","11010","00101"};
byte[]bytes = new byte[binArray.length];
for (int i=0; i< binArray.length;i++){
bytes[i] = Byte.parseByte(binArray[i],2);
}
I may be missing something here. Hence request your guidance.
There seems to be a very general misunderstanding.
In some sense, all data is stored "in binary form". Particularly, the integral numerical values, like byte, short, int etc., are internally all stored in binary form. This internal representation is known as the Two's complement form.
(For floating point numbers, the representation is a bit more complicated: It's the IEEE 754 representation - still, they are stored in binary form)
The key issue that has likely lead to your question is: When you just print a number, or convert them to a string, with
System.out.println(someByte);
or
String s = String.valueOf(someByte);
then by default, the decimal form is printed. Mainly because this is the most "natural" and most readable form for humans.
You can pass your bytes to Integer.html#toBinaryString to create the string representation of their binary representation:
System.out.println(Integer.toBinaryString(someByte));
Well, the JVM, just like any program doesn't store numeric values as "decimal" or "hexadecimal". It's just a bit-pattern in memory. Your debugger displays the value in decimal format. This is just for convenience, since most people prefer decimal to binary format for readability. For the computer itself it's just a bit-string of 8 bits length, stored as binary-value.
While debugging the array just to see how it is stored, I could see them stored internally as decimals. Why is that?
You are seeing the byte value displayed in decimal. The debugger is not displaying the internal value.
When it gets interpreted as bytecode I am assuming it will get converted as binary. Then why not store in binary in the first place.
It is stored in binary.

How to convert Floating Point into Decimal Java

I would like to write a program that converts a floating point binary number into decimal. I know how to convert a normal binary number into decimal but I would like to convert a floating point binary into decimal with a mantissa of 10 and a exponent of 6.
At the same time I would like to use input to gain the binary value e.g.
System.out.println("Enter a binary number");
Then use a scanner to gain input. Is this possible?
Given a String like "101011.1011", one approach is:
Split the string into two bits using String.split.
Use the BigInteger constructor BigInteger(String s, int radix) with radix == 2 to get the two BigIntegers representing 1010111011 and 10000.
Use the BigDecimal constructor taking BigInteger to convert to BigDecimal.
Divide the answers.
I don't know if there's a simpler way.

What kind of number is aa568 and how do I convert to it from decimal in Java?

I assume aa568 uses a different base than 10.
What type of number is this most likely?
And how do you convert a decimal number into this base using Java?
Could it be hexadecimal? If it is then just precede that by 0x ie. 0xaa568.
Assuming it is hexadecimal (0-9 + A-F instead of 0-9), you can convert it from hex to decimal as follows:
int i = Integer.parseInt(hexStr,16);
Where 16 is the base of the number system. Decimal is base 10, hexadecimal is base 16.
And back from decimal to hexadecimal:
String hexStr = Integer.toHexString(i);
Converting a number into a hexadecimal string can be done by the Integer.toHexString() method:
int number = 697704;
System.out.println(Integer.toHexString(number));
will print "aa568".
I'd assume that it's hexadecimal and more typically written 0xAA568 which is decimal 697704.
If you're asking how you would print a decimal number in a hexadecimal representation using Java ... see this stackoverflow article.
What kind of number may depend on the context, of none of the above fits, then it may be an integer sequence, I looked it up in the online encyclopedia of known integer sequences. Example a000045 is a Fibonacci sequence, (Formerly M0692 N0256), if it has to do with math it may also be a library reference to a paper written by AA. Only the Context can tell. Mathnet.ru had one reference "The behavior of the Lebesgue constants of two-dimensional Fourier sums over polygons" which fits the questioned number aa568.

Categories