kotlin-graphics/kotlin-unsigned

toBinaryString returns 0s

TWiStErRob opened this issue · 4 comments

Code: println((-71777214294589696L).toBinaryString())
Output: 0000000000000000000000000000000000000000000000000000000000000000

Tried .toUnsignedString(2), toHexString() as well, it's less 0s.

.toUlong().toString() gives a good decimal representation, but I need binary; UTypes don't have toString(radix) methods.

.toUlong().toBigInt().toString(2) gives the right value, but pretty tedious.

.toUBigInt().toString(2) is based on toUnsignedString(10), luckily that's not using toUnsignedString0, so it works.

The bug is in the konversion of formatUnsignedLong from the JDK: when you introduced _this, you forgot to replace the implicit this.toInt() and mask inside digits[], it should be _this.toInt() and mask.

Hi Róbert,

thanks for opening the issue and suggesting the fix, I quickly applied it and it worked.

I'll review all the other call and problems you mentioned with calm.

Btw, push request are welcome for the next time if you want to do it directly.

Shall I publish a release for this fix?

The other issues are not really issues, I was just looking to work around this bug.
I guess the only thing you need to look at is "UTypes don't have toString(radix) methods."

No need to push any quick, I decided to just go with fun Int.toBinaryHashString() = java.lang.Integer.toBinaryString(this) instead of pulling your whole lib in, I'm on desktop and targeting latest Java so have the API I need.

Added toString(radix) in 1.3 branch

A little late but better than never :p