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:

'Response from server: eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TMmGa/bG6g7RVU=

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"
Copying updated assets files (10)
Application_Start
Can't find keyplane that supports type 5 for keyboard iPhone-PortraitChoco-PhonePad; using 2024220450015396792_PortraitChoco_iPhone-Complex-Pad_Default
Can't find keyplane that supports type 5 for keyboard iPhone-PortraitChoco-PhonePad; using 2024220450015396792_PortraitChoco_iPhone-Complex-Pad_Default
Application_Active
Response from server: eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TSmGa/bG6g7RVU=
Error occurred on line: 156 (act_login)
Error decoding data as string.
Stack Trace: (
CoreFoundation <redacted> + 148
libobjc.A.dylib objc_exception_throw + 56
CoreFoundation <redacted> + 0
Ordine Pizza -[B4ICommon BytesToString::::] + 344
CoreFoundation <redacted> + 144
CoreFoundation <redacted> + 292
Ordine Pizza +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
Ordine Pizza -[B4IShell runMethod:] + 448
Ordine Pizza -[B4IShell raiseEventImpl:method:args::] + 1260
Ordine Pizza -[B4IShellBI raiseEvent:event:params:] + 1408
Ordine Pizza -[B4ICommon CallSub4::::] + 344
Ordine Pizza -[B4ICommon CallSub2::::] + 360
Ordine Pizza -[b4i_httpjob _complete:] + 212
Ordine Pizza -[b4i_httputils2service _completejob::::] + 436
Ordine Pizza -[b4i_httputils2service _hc_responsesuccess::] + 164
CoreFoundation <redacted> + 144
CoreFoundation <redacted> + 292
Ordine Pizza +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
Ordine Pizza -[B4I raiseEvent:event:params:] + 564
Ordine Pizza __61-[B4IHttp URLSession:downloadTask:didFinishDownloadingToURL:]_block_invoke + 268
libdispatch.dylib <redacted> + 16
libdispatch.dylib <redacted> + 68
libdispatch.dylib <redacted> + 16
libdispatch.dylib <redacted> + 1016
CoreFoundation <redacted> + 12
CoreFoundation <redacted> + 2012
CoreFoundation CFRunLoopRunSpecific + 436
GraphicsServices GSEventRunModal + 100
UIKit UIApplicationMain + 236
Ordine Pizza main + 124
libdyld.dylib <redacted> + 4
)
 
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:

Application_Start
Application_Active
Can't find keyplane that supports type 5 for keyboard iPhone-PortraitChoco-PhonePad; using 2024220450015396792_PortraitChoco_iPhone-Complex-Pad_Default
Can't find keyplane that supports type 5 for keyboard iPhone-PortraitChoco-PhonePad; using 2024220450015396792_PortraitChoco_iPhone-Complex-Pad_Default
Response from server: eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TSmGa/bG6g7RVU=
(empty string)
 
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:

Response from server: eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TSmGa/bG6g7RVU=
x�N�IM.Q�RH+��U(�,(���A��
Select * from pippo


Same code in B4i i have this result:
Application_Start
Can't find keyplane that supports type 5 for keyboard iPhone-PortraitChoco-PhonePad; using 2024220450015396792_PortraitChoco_iPhone-Complex-Pad_Default
Can't find keyplane that supports type 5 for keyboard iPhone-PortraitChoco-PhonePad; using 2024220450015396792_PortraitChoco_iPhone-Complex-Pad_Default
Application_Active
Response from server: eJxVj8EKwjAQRH+l9CySmqrgLaiHgKUgxYtIKMlaAm22JClCxX93Ix70tPBmdpi5PnNr1BTBRch3ecHLfJHIaOcZvG0TWxHSGCIqjS5A5xJlS8aIRzuiMvZPIexwgJ8MN/X9F2rs0iVfJc77OhOV3Mv0MQ3gUUXo4Y7OaiQH5yXnm23B1qmUM9ZTYhIuUmSH4ymrRNOIppY16WMbwgO9+TSmGa/bG6g7RVU=
Error occurred on line: 160 (act_login)
Error decoding data as string.
Stack Trace: (
CoreFoundation <redacted> + 148
libobjc.A.dylib objc_exception_throw + 56
CoreFoundation <redacted> + 0
Ordine Pizza -[B4ICommon BytesToString::::] + 344
CoreFoundation <redacted> + 144
CoreFoundation <redacted> + 292
Ordine Pizza +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
Ordine Pizza -[B4IShell runMethod:] + 448
Ordine Pizza -[B4IShell raiseEventImpl:method:args::] + 1260
Ordine Pizza -[B4IShellBI raiseEvent:event:params:] + 1408
Ordine Pizza -[B4ICommon CallSub4::::] + 344
Ordine Pizza -[B4ICommon CallSub2::::] + 360
Ordine Pizza -[b4i_httpjob _complete:] + 212
Ordine Pizza -[b4i_httputils2service _completejob::::] + 436
Ordine Pizza -[b4i_httputils2service _hc_responsesuccess::] + 164
CoreFoundation <redacted> + 144
CoreFoundation <redacted> + 292
Ordine Pizza +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1624
Ordine Pizza -[B4I raiseEvent:event:params:] + 564
Ordine Pizza __61-[B4IHttp URLSession:downloadTask:didFinishDownloadingToURL:]_block_invoke + 268
libdispatch.dylib <redacted> + 16
libdispatch.dylib <redacted> + 68
libdispatch.dylib <redacted> + 16
libdispatch.dylib <redacted> + 1016
CoreFoundation <redacted> + 12
CoreFoundation <redacted> + 2012
CoreFoundation CFRunLoopRunSpecific + 436
GraphicsServices GSEventRunModal + 100
UIKit UIApplicationMain + 236
Ordine Pizza main + 124
libdyld.dylib <redacted> + 4
)
 
Last edited:
Upvote 0
Top