Results 1 to 16 of 16

Thread: Optimizing byte array to string encoder

Threaded View

  1. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Optimizing byte array to string encoder

    Assuming the algorithm I posted is correct...

    Code:
            Dim buf(255) As Byte 'test buffer
    
            'generate all possible byte values for testing
            For bv As Integer = 0 To 255
                buf(bv) = CByte(bv)
            Next
    
    
            Dim eqAsByte As Byte = Asc("=")
            Dim critCH() As Byte = New Byte() {0, _
                                               Asc(ControlChars.Tab), _
                                               Asc(ControlChars.Lf), _
                                               Asc(ControlChars.Cr), _
                                               eqAsByte}
    
            Dim encodedBuf As New List(Of Byte)
            'encode
            For Each byt As Byte In buf 'fetch the character
                Dim addByte As Integer
                If Not critCH.Contains(byt) Then 'is it critical
                    'no
                    addByte = byt + 42
                Else
                    'yes, is a critical? character
                    encodedBuf.Add(eqAsByte) 'add equal sign to output
                    addByte = byt + 64
                End If
                addByte = addByte And 255
                encodedBuf.Add(CByte(addByte))
            Next
    
            Dim outBuf() As Byte = encodedBuf.ToArray
            'Debug.WriteLine(System.Text.ASCIIEncoding.ASCII.GetChars(encodedBuf.ToArray))
    edit - Wow!
    Last edited by dbasnett; Feb 4th, 2011 at 04:23 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width