iOS Question [RESOLVED] DecodeBase64 Error

MarcoRome

Expert
Licensed User
Longtime User
Hi all, i have this code that work without problem into android:

B4X:
Sub decode(texto As String) As String
    Dim strResult As String
    Dim cs As CompressedStreams
    Dim su As StringUtils
    Dim bt() As Byte
    Dim bc As ByteConverter
    Try
        'Response from server: eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TMmGa/bG6g7RVU=
        bt = su.DecodeBase64(texto)
        Dim xx As String  = bc.StringFromBytes(bt, "UTF8")
        Log(xx) 'Empty String <----- Why ???
        bt = cs.DecompressBytes(bt, "zlib")
        
        strResult = bc.StringFromBytes(bt, "UTF8")
        Return strResult
    Catch
        Log(LastException)
        Return ""
    End Try
End Sub

In B4i DecodeBase64 give me result "Empty String" also if server response same thing that in android:


Any suggestion ?
Thank you very much
Marco
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The encoded data is not a string. You can test it with:
B4X:
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64("eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TMmGa/bG6g7RVU=")
Log(BytesToString(b, 0, b.Length, "utf8"))

You cannot treat raw bytes as a string.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Hi Erel thank you for your reply. I think that the problem is into DecodeBase64 iOS
For 2 Reason:

1. The same code already work in B4A in different project without problem ( already for a long time )
This is movie same project with same result without error in B4A ( Time ago i wrote also Snippet in B4A: https://www.b4x.com/android/forum/t...e64-gzcompress-via-okhttputils.73246/#content )



2. I tried your code:
B4X:
        Dim su As StringUtils
        Dim b() As Byte = su.DecodeBase64("eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TMmGa/bG6g7RVU=")
        Log(BytesToString(b, 0, b.Length, "utf8"))

Crash with "Error decoding data as string"
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is better to post the code and error message as text instead of these videos.

This code fails:
B4X:
Dim su As StringUtils
Dim b() As Byte = su.DecodeBase64("eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TMmGa/bG6g7RVU=")
Dim cs As CompressedStreams
Dim uncompressed() As Byte = cs.DecompressBytes(b, "zlib")
This means that the data encoded in the base 64 string was not compressed with zlib.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
The result DecodeBase64 is "empty string"
infact the xx return (empty string )

Code:
B4X:
Sub decode(texto As String) As String
    Dim strResult As String
    Dim cs As CompressedStreams
    Dim su As StringUtils
    Dim bt() As Byte
    Dim bc As ByteConverter
    Try
        Dim su As StringUtils
        bt = su.DecodeBase64(texto)
        Dim xx As String  = bc.StringFromBytes(bt, "UTF8")
        Log(xx) 'Empty String <----- Why ???
        bt = cs.DecompressBytes(bt, "zlib")
        strResult = bc.StringFromBytes(bt, "UTF8")
        Return strResult
        
        strResult =  su.EncodeBase64(cs.CompressBytes(texto.GetBytes("UTF8"), "zlib"))
    Catch
        Log(LastException)
        Return ""
    End Try
End Sub

Result:

 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
ok. Reset.

Try only this code:

B4X:
Dim strResult As String
Dim cs As CompressedStreams
Dim su As StringUtils
Dim bt() As Byte
Dim bc As ByteConverter

'String that i want encode/decode
Dim testo As String = "Select * from pippo"
'Here encode
Dim example_encode As String =  su.EncodeBase64(cs.CompressBytes(testo.GetBytes("UTF8"), "zlib"))

'Here Decode
bt = su.DecodeBase64(example_encode)
Log(BytesToString(bt, 0, bt.Length, "utf8"))
bt = cs.DecompressBytes(bt, "zlib")
strResult = bc.StringFromBytes(bt, "UTF8")
Return strResult

in B4A i have this correct result:



Same code in B4i i have this result:
 
Last edited:
Upvote 0