Convert VB6 yDecoder to B4a

potman100

Active Member
Licensed User
Longtime User
Hi

I have a ydecoder function that I use in VB6, I need to convert it to run under b4a, I think its fairly straight forward, just need to change a couple of string replace lines, but there is 1 part I have no idea on, so hope somebody can help.

This is the VB6 Function
B4X:
Public Function yDecode(sString As String) As String

    Dim bOut() As Byte, bIn() As Byte, bTest As Byte, bEscaped As Boolean, lPos As Long, lChar As Long

    sString = Replace(sString, vbCr, vbNullString)      'Get rid of the vbCrLfs.  These could be in...
    sString = Replace(sString, vbLf, vbNullString)      'either order.

    bIn = StrConv(sString, vbFromUnicode)               'Load the input byte array.
    ReDim bOut(UBound(bIn))                             'There is no case where the output will be larger.
    
    For lChar = 0 To UBound(bIn)
        If bIn(lChar) <> 61 Then
            bTest = ((bIn(lChar) + 256) - 42) Mod 256
            If bEscaped Then
                bTest = ((bTest + 256) - 64) Mod 256
                bEscaped = False
            End If
            bOut(lPos) = bTest
            lPos = lPos + 1
        Else
            bEscaped = True
        End If
    Next lChar

    ReDim Preserve bOut(lPos - 1)                       'Truncate the unused portion of the buffer.
    yDecode = StrConv(bOut, vbUnicode)                  'Convert back to a string and return.

End Function

And this is what I have so far with b4a

B4X:
Sub ydecoder(iData As String) As String 
Dim bIn() As Byte, bTest As Byte, bEscaped As Boolean, lPos As Long, lChar As Long

   iData = iData.Replace(Chr(10),Null)
   iData = iData.Replace(Chr(13),Null)
   
   bIn = iData.GetBytes("UTF-8")

Dim bOut(bIn.Length) As Byte


'*********** NOT SURE IF THIS WILL WORK, BUT I THINK IT WILL ************

    For lChar = 0 To bIn.Length 
        If bIn(lChar) <> 61 Then
            bTest = ((bIn(lChar) + 256) - 42) Mod 256
            If bEscaped Then
                bTest = ((bTest + 256) - 64) Mod 256
                bEscaped = False
            End If
            bOut(lPos) = bTest
            lPos = lPos + 1
        Else
            bEscaped = True
        End If
    Next lChar

'*********** NOT SURE IF THIS WILL WORK, BUT I THINK IT WILL ************




End Sub

Im unsure about the last 2 lines :

B4X:
ReDim Preserve bOut(lPos - 1)                       'Truncate the unused portion of the buffer.
yDecode = StrConv(bOut, vbUnicode)                  'Convert back to a string and return.

Thanks in advance for any help.

Regards

Potman100
 

potman100

Active Member
Licensed User
Longtime User
Hi

yEnc is an encoding method which offers efficient and proper transmission for binaries on Usenet.

99% of Usenet binaries use this method of encoding when uploading to Usenet.

Im playing with an Nntp program.

Regards

Potman100
 
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Hi nfordbscndrd

Thanks for the info, I did not know about this post, Ill have a look and see if I can convert it.

Regards

Potma100
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
For the last lines try these:
B4X:
  Next

  Dim bc as ByteConverter
  Dim yDecode as String
  yDecode = bc.StringFromBytes(bOut, "UTF-8")
  Return yDecode
End Sub
No iteration variable after Next !
Uses the ByteConverter library.

Best regards.
 
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Hi Klaus,

Firstly thanks for the reply

Sorry this is a bit long but wanted to give you the whole picture.

I compile the data from an AsyncStreams, when I recieve data on the stream
I convert is from a byte array to a string :

Dim msg As String
msg = BytesToString(buffer, 0, buffer.Length, "UTF-8")

There can be multi packets of data to create the final raw data so I add the
msg data to a string :

nBuffer = nBuffer & msg

when the data is complete I pass it to the ydecoder sub, there is a bit of cleaning
required, removing chr(10) & chr(13) from the string data, then its converted in to
a byte array for decoding :

bIn = iData.GetBytes("UTF-8")


Vb Source :



Vb Decoded data :



B4a Source :


B4a Decoded data :


I think its something to do with Character Sets ?

Any Idea's ?

Regards

Potman100
 
Last edited by a moderator:
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Hi Klaus

I found one, not exactly sure how it works !

When reading the asyncstream I found that using :

msg = BytesToString(buffer, 0, buffer.Length, "WINDOWS-1252")

created the same data as in VB6, but there was still an issue in the decoding
of the data, so I passed the WINDOWS-1252" encoded buffer to the decoder as a string
and then converted it to a byte array :

bIn = iData.GetBytes("WINDOWS-1252")

all appeared to work until I tried to convert it back to a string, and then the data was all messed up.

The soloution was not to convert it to a string, but use a RandomAccessFile to write the bytes to the
output file and it works 100%.

Thanks for your time and help.

Regards

Potman
 
Upvote 0
Top