udg Expert Licensed User Longtime User Nov 1, 2021 #1 Hi, I'd like an additional function that returns the complete string of bits instead of just the significant ones. Something like: Code: 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 Code: Log(Bit.ParseInt(ToBinaryString2(test),2)) Note: unexpectedly ParseInt fails for negatives. For example: Code: 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: Nov 1, 2021
Hi, I'd like an additional function that returns the complete string of bits instead of just the significant ones. Something like: Code: 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 Code: Log(Bit.ParseInt(ToBinaryString2(test),2)) Note: unexpectedly ParseInt fails for negatives. For example: Code: 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
Erel B4X founder Staff member Licensed User Longtime User Nov 2, 2021 #2 You don't need anything special to create such a string. Code: Sub ToBinaryString(set As B4XBitSet) As String Dim sb As StringBuilder sb.Initialize For i = set.Size-1 To 0 Step -1 sb.Append(IIf(set.Get(i), "1", "0").As(String)) Next Return sb.ToString End Sub
You don't need anything special to create such a string. Code: Sub ToBinaryString(set As B4XBitSet) As String Dim sb As StringBuilder sb.Initialize For i = set.Size-1 To 0 Step -1 sb.Append(IIf(set.Get(i), "1", "0").As(String)) Next Return sb.ToString End Sub
udg Expert Licensed User Longtime User Nov 2, 2021 #3 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: Code: 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
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: Code: 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 Nov 2, 2021 #4 udg said: The idea behind my wish was to make it available as a standard provision for Bit. Click to expand... 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. udg said: Anyway, what about the ParseInt function when using negatives? Click to expand... This is related to the way the underlying numeric parser works. You can use this code instead: Code: 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 said: The idea behind my wish was to make it available as a standard provision for Bit. Click to expand... 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. udg said: Anyway, what about the ParseInt function when using negatives? Click to expand... This is related to the way the underlying numeric parser works. You can use this code instead: Code: 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 Nov 2, 2021 #5 Converting the bit set to a string is an esoteric feature. B4XBitSet is expected to be used with large set Click to expand... 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. Click to expand... 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
Converting the bit set to a string is an esoteric feature. B4XBitSet is expected to be used with large set Click to expand... 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. Click to expand... 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