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]
Re: [02/03] byte to character conversion issue
Final code:
VB Code:
Dim myInBuffer(20000) As Byte
Dim myOutBuffer(20000) As Byte
Dim outCount As Integer
Dim inPos As Integer
Dim encoding As New System.Text.UnicodeEncoding()
Dim decode(0) As Byte
Dim backstrign = FDXClientAPI.FedExAPITransaction(nRoute, Request.ToString(), IP, nPort)
Dim toBytes() As Byte = encoding.GetBytes(FDXClientAPI.FedExAPITransaction(nRoute, Request.ToString(), IP, nPort))
Dim bByte 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 ErrInfo As String
Dim sPrevious As String
Dim iPrevious As Integer
Dim iCharacter As Integer
Dim objWriter As System.IO.FileStream
objWriter = New System.IO.FileStream("cfsout.pdf", System.IO.FileMode.Create)
Try
inPos = 0
For Each bByte In toBytes
If bByte <> 0 Then
sTmp = Chr(bByte)
Select Case bByte
Case 37
'start to translate the next 3 bytes and convert
iFound = 1
iEncoding += 1
Case 34
If iFound = 1 Then
'End of buffer so exit for loop
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"
decode(0) = 37
objWriter.Write(decode, 0, 1)
Case "%00"
decode(0) = 0
objWriter.Write(decode, 0, 1)
Case "%22"
decode(0) = 34
objWriter.Write(decode, 0, 1)
End Select
iCharacter += 1
iEncoding = 0
sTmpHolder = String.Empty
inPos += 3
End If
Else
objWriter.Write(toBytes, inPos, 1)
inPos += 1
End If
Else
inPos += 1
End If
Else
inPos += 1
End If
Next
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
objWriter.Close()
txtOutput.Text = backstrign