Results 1 to 2 of 2

Thread: RESOLVED - [02/03] byte to character conversion issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Resolved RESOLVED - [02/03] byte to character conversion issue

    Taking a buffer and putting into a byte array and looping through each character (translating a few) and then outputing bytes. I am finding that certain bytes are not being converted properly.

    I am able to view the final result a different way by going through the website and can compare the output.

    Output from the code below:
    Code:
    ?py;£¢ã]?Ì$ñLPä0P	S??<âÓï.ï¾Ê²îüþ`f?gçË¿?
    Same line but from file from web:
    Code:
    ’py;£¢ã]‹Ì$ñLPä0P	S„€<âÓï.ï¾Ê²îüþ`fžgçË¿™
    You can see the first and last character is ? on my output but should be different. Is it something with converting back with chr()?

    My code producing output
    [Highlight=VB]
    Dim toBytes() As Byte = ASCIIEncoding.Default.GetBytes(FDXClientAPI.FedExAPITransaction(nRoute, Request.ToString(), IP, nPort))
    Dim backstrign = ASCIIEncoding.Default.GetString(toBytes)

    Dim SByte As Byte
    Dim sTmp As String
    Dim iFound As Integer
    Dim iEnd As Integer
    Dim sTmpHolder As String
    Dim iEncoding As Integer
    Dim sOutput As String
    Dim objWriter As StreamWriter
    Dim ErrInfo As String
    objWriter = New StreamWriter("C:\Output.pdf")

    Try
    For Each SByte In toBytes

    sTmp = Chr(SByte)

    Select Case SByte
    Case 37
    iFound = 1
    iEncoding += 1
    Case 34
    If iFound = 1 Then
    Exit For
    End If
    End Select

    If iFound = 1 Then
    If iEncoding > 0 Then
    sTmpHolder &= sTmp
    If sTmpHolder.Length = 3 Then
    Select Case sTmpHolder
    Case "%25"
    objWriter.Write("%")
    Case "%00"
    objWriter.Write(vbNullChar)
    Case "%22"
    objWriter.Write("""")
    End Select
    iEncoding = 0
    sTmpHolder = String.Empty
    End If
    Else
    objWriter.Write(sTmp)
    End If
    End If
    Next

    Catch Ex As Exception
    ErrInfo = Ex.Message
    End Try

    objWriter.Close()

    txtOutput.Text = backstrign
    [Highlight=VB]
    Last edited by lleemon; Aug 4th, 2006 at 11:12 AM. Reason: Resolved

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: [02/03] byte to character conversion issue

    Final code:
    VB Code:
    1. Dim myInBuffer(20000) As Byte
    2.                 Dim myOutBuffer(20000) As Byte
    3.                 Dim outCount As Integer
    4.                 Dim inPos As Integer
    5.                 Dim encoding As New System.Text.UnicodeEncoding()
    6.                 Dim decode(0) As Byte
    7.  
    8.                 Dim backstrign = FDXClientAPI.FedExAPITransaction(nRoute, Request.ToString(), IP, nPort)
    9.                 Dim toBytes() As Byte = encoding.GetBytes(FDXClientAPI.FedExAPITransaction(nRoute, Request.ToString(), IP, nPort))
    10.  
    11.                 Dim bByte As Byte
    12.                 Dim sTmp As String
    13.                 Dim iFound As Integer
    14.                 Dim iEnd As Integer
    15.                 Dim sTmpHolder As String
    16.                 Dim iEncoding As Integer
    17.                 Dim sOutput As String
    18.                 Dim ErrInfo As String
    19.                 Dim sPrevious As String
    20.                 Dim iPrevious As Integer
    21.                 Dim iCharacter As Integer
    22.                 Dim objWriter As System.IO.FileStream
    23.                 objWriter = New System.IO.FileStream("cfsout.pdf", System.IO.FileMode.Create)
    24.  
    25.                 Try
    26.                     inPos = 0
    27.                     For Each bByte In toBytes
    28.                         If bByte <> 0 Then
    29.                             sTmp = Chr(bByte)
    30.  
    31.                             Select Case bByte
    32.                                 Case 37
    33.                                     'start to translate the next 3 bytes and convert
    34.                                     iFound = 1
    35.                                     iEncoding += 1
    36.                                 Case 34
    37.                                     If iFound = 1 Then
    38.                                         'End of buffer so exit for loop
    39.                                         Exit For
    40.                                     End If
    41.                             End Select
    42.  
    43.                             If iFound = 1 Then
    44.                                 If iEncoding > 0 Then
    45.                                     sTmpHolder &= sTmp
    46.                                     If sTmpHolder.Length = 3 Then
    47.                                         Select Case sTmpHolder
    48.                                             Case "%25"
    49.                                                 decode(0) = 37
    50.                                                 objWriter.Write(decode, 0, 1)
    51.                                             Case "%00"
    52.                                                 decode(0) = 0
    53.                                                 objWriter.Write(decode, 0, 1)
    54.                                             Case "%22"
    55.                                                 decode(0) = 34
    56.                                                 objWriter.Write(decode, 0, 1)
    57.                                         End Select
    58.                                         iCharacter += 1
    59.                                         iEncoding = 0
    60.                                         sTmpHolder = String.Empty
    61.                                         inPos += 3
    62.                                     End If
    63.                                 Else
    64.                                     objWriter.Write(toBytes, inPos, 1)
    65.                                     inPos += 1
    66.                                 End If
    67.                             Else
    68.                                 inPos += 1
    69.                             End If
    70.                         Else
    71.                             inPos += 1
    72.                         End If
    73.                     Next
    74.  
    75.                 Catch Ex As Exception
    76.                     ErrInfo = Ex.Message
    77.                 End Try
    78.  
    79.                 objWriter.Close()
    80.  
    81.  
    82.                 txtOutput.Text = backstrign

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