Wish Bit.ToBinaryString2

udg

Expert
Licensed User
Longtime User
Hi,
I'd like an additional function that returns the complete string of bits instead of just the significant ones.
Something like:
B4X:
Sub ToBinaryString2(N As Int) As String
    Dim filler As String = "00000000000000000000000000000000"
    Dim sTest As String = Bit.ToBinaryString(N)
    Return filler.SubString(sTest.Length)&sTest
End Sub

To return from that string representation to its original Int value ,we already have function ParseInt. so
B4X:
Log(Bit.ParseInt(ToBinaryString2(test),2))

Note: unexpectedly ParseInt fails for negatives. For example:
B4X:
Dim test As Int = -2
Dim s As String = Bit.ToBinaryString(test)
Log(Bit.ParseInt(s,2))

Why did I post this wish? I was studying class B4xBitSet (which I like it a lot) and wanted to save the "block" entries (which for B4A/J are Ints) to their BinaryString representation in order to then save them in a Text field (or similar) in a database.
I'm aware of a function that returns booleans for each bit in the set in order to then use the Serializator, but once the set data array will be exposed (it will be a separate wish) it will be easier to put all the "blocks" together in a long string (and viceversa).

udg
 
Last edited:

udg

Expert
Licensed User
Longtime User
Thanks Erel. The idea behind my wish was to make it available as a standard provision for Bit.
I know that we can "extract and modify" code published as b4xlibs.

Anyway, what about the ParseInt function when using negatives? I repeat here the code in case you missed it:
B4X:
Dim test As Int = -2
Dim s As String = Bit.ToBinaryString(test)
Log(Bit.ParseInt(s,2))
This code fails with error:
java.lang.NumberFormatException: For input string: "11111111111111111111111111111110"

udg
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The idea behind my wish was to make it available as a standard provision for Bit.
Converting the bit set to a string is an esoteric feature. B4XBitSet is expected to be used with large sets. In such cases it doesn't make sense to convert the data to a string.
If you need it then use the code I posted.

Anyway, what about the ParseInt function when using negatives?
This is related to the way the underlying numeric parser works. You can use this code instead:
B4X:
Sub ParseSignedBinary (s As String) As Int
    If s.Length = 32 And s.CharAt(0) = "1" Then
        Dim i As Int = Bit.ParseInt(s.SubString(1), 2)
        Return 0x80000000 + i
    Else
        Return Bit.ParseInt(s, 2)
    End If
End Sub
 

udg

Expert
Licensed User
Longtime User
Converting the bit set to a string is an esoteric feature. B4XBitSet is expected to be used with large set
I see your point and I agree. For large sets it will be mostly unusable.
This is related to the way the underlying numeric parser works.
Maybe a note of caution on ParseInt hint text would be useful.
I can understand the difficulty when designing the language and its "core" libs, whether to strictly adhere to java signed data types versus a more general bit manipulation paradigm.
Anyway, the good news is that we always have a solution handy :)

Thank you again
 
Top