How to convert BigInteger into binary?
13 views (last 30 days)
Error using dec2bin (line 24)
D must be numeric.
I have tried this
ans = '10011100000010011000000011110110100110100000111111111000000000000000000000000000000000000000000000000000'
but it is not giving right output
The output should be
John D'Errico on 12 Mar 2022
Edited: John D'Errico on 12 Mar 2022
You would not expect dec2bin to do it, when you convert the number oto a double. It will fail above 2^53-1, and that number is surely too large. Well, you SHOULD not expect it to work, if you understand what a double means, and you DID convert it to a double. The clue as to why it failed is of course in the output you did get. The first 50 bits or so were correct, but then you see entirely zeros. That suggests the problem you had was in the conversion to a double, which threw away all those lower order bits, directly into the bit bucket.
However, this should do it:
B = reshape(dec2bin(double(toByteArray(a)))',1,);
Note that it may have leading zeros on the highest order byte, because the Java number gets returned as a vector of bytes. I suppose you could zap them away.
B = B(find(B == '1',1,'first'):end)
Is that correct?
C = dec2bin(sym('12362534624362643243256346523462'))