Android Question How to convert a PDF/JPG on Windows(VB6) to write it in a B4A File?

Michael Müller Anywhere

Member
Licensed User
Longtime User
Hello,

For a few days I have been trying to transfer PDF / JPG files via socket from VB6 to B4A. That is working.

But I cannot save this data correctly 1:1 on B4A

I've already tried:
- Windows encodeBase64 --> B4A:decodeBase64 (I tried different Base64 encoding routines on Windows)
- simple hex-encoded (Windows ansi) --> B4A: ASCII and UTF8-Format
- AES_decrypt / encrypt

Either runtime errors occur or only NULL characters are stored when I try to save it in a file.
I used these two ways:
- out.WriteBytes(data64, 0, data64.Length)
- File.WriteString(path, file, s)

Does anyone know a way to encode the file on windows so that it can later be properly decoded in B4A to save it in a file?


By the way, the direction B4A to Windows is working when I use a simple hex-encoding:
Dim Bconv As ByteConverter
datastring = Bconv.HexFromBytes(data)



Thank you!
 

Michael Müller Anywhere

Member
Licensed User
Longtime User
Puuuh, I find it out myself. For all:

VB6-Code:
Dim bArray() As Byte

Private Declare Function CryptBinaryToString Lib "Crypt32.dll" Alias _
                        "CryptBinaryToStringW" (ByRef pbBinary As Byte, _
                        ByVal cbBinary As Long, ByVal dwFlags As Long, _
                        ByVal pszString As Long, ByRef pcchString As Long) As Long

Private sub mySendfile(filename)
Dim n As Long
Dim DateiNr As Integer
dim c as string  'string I want to send via socket

  n = FileLen(filename)
  ReDim bArray(n - 1)
  DateiNr = FreeFile
  Open filename For Binary Access Read As #DateiNr
      Get #DateiNr, , bArray()
  Close #DateiNr
  c = myBase64Encode(bArray)
End sub

Private Function myBase64Encode(ByRef byt() As Byte) As String
    Const CRYPT_STRING_BASE64 As Long = 1
    Const CBS As String = "CryptBinaryToString"
    Const Routine As String = "Base64.Base64Encode"
    Dim lLen As Long
    'Determine Base64 output String length required.
    If CryptBinaryToString(byt(0), UBound(byt) + 1, CRYPT_STRING_BASE64, StrPtr(vbNullString), lLen) = 0 Then
        'RaiseEvent Error(Err.LastDllError, CBS, Routine)
        Err.Raise Err.LastDllError, CBS, Routine
        GoTo ReleaseHandles
    End If
    'Convert binary to Base64.
    Dim sBase64Buf As String
    sBase64Buf = String$(lLen - 1, Chr$(0))
    If CryptBinaryToString(byt(0), UBound(byt) + 1, CRYPT_STRING_BASE64, StrPtr(sBase64Buf), lLen) = 0 Then
        'RaiseEvent Error(Err.LastDllError, CBS, Routine)
        Err.Raise Err.LastDllError, CBS, Routine
        GoTo ReleaseHandles
    End If
    myBase64Encode = Left$(sBase64Buf, lLen - 2)
ReleaseHandles:
End Function

B4A-Code:
Dim s64 as string  'the input via socket
Dim su As StringUtils
Dim data64() As Byte = su.decodeBase64(s64)
Dim out As OutputStream = File.OpenOutput(pfad , datei,  False)
out.WriteBytes(data64, 0, data64.Length)
out.Close

It works with PDF and JPG and big files (i tried a file with 3.8 MByte)
 
Upvote 0
Top