Results 1 to 7 of 7

Thread: Two Encoding decode...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Two Encoding decode...

    Let's suppose a string. I use
    VB Code:
    1. dim abyte() as byte
    2. abyte=encoding.unicode.getbytes(mystr)
    and it encode it. Now, if I encode again into another array, I get the char array i would get if I translate directly the string to char array. Fun or am I wrong ??
    Learn, this is the Keyword...

  2. #2
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    What do you mean by "encode again into another array"? To get the characters back, you have to decode them, not encode them again.

    Using encoding.unicode.getchars will get the characters:
    VB Code:
    1. Dim mychars() As Char = Encoding.Unicode.GetChars(abyte)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Sure I have to decode it! The problem was when I tried to convert it to a String and again to bytes... I got a bad rapresentation. Don't know why, but I got 2 different arrays...
    Learn, this is the Keyword...

  4. #4
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    Can you post the code you used to do this?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    Look at this thread for understanding the problem I had.
    http://www.vbforums.com/showthread.p...hreadid=244481
    VB Code:
    1. Public Function DataSetToString(ByRef ds As DataSet) As String
    2.         Dim ms As MemoryStream = New MemoryStream()
    3.         Dim bf As New BinaryFormatter()
    4.  
    5.         bf.Serialize(ms, ds)
    6.         ms.Position = 0
    7.  
    8.         Dim sr As Byte()
    9.         sr = ms.ToArray()
    10.  
    11.         Return Encoding.ASCII.GetString(sr) 'or unicode, is the same
    12.     End Function
    13.  
    14.     Public Function StringToDataSet(ByVal data As String) As DataSet
    15.         Dim ms As New IO.MemoryStream()
    16.         Dim sw As New IO.StreamWriter(ms)
    17.         Dim bf As New BinaryFormatter()
    18.  
    19.         sw.Write(data)
    20.         sw.Flush()
    21.         ms.Position = 0
    22.  
    23.         Dim ds As New DataSet()
    24.         ms.Position = 0
    25.         ds = bf.Deserialize(ms)
    26.  
    27.         Return ds
    28.     End Function
    Those are the functions I used to serialize and deserialize the DataSet. One in the server, the other in the client. The two Encoding are: one in these functions, the other when I send the string through the network.
    Finally I found the problem: the last line of the DataSetToString function returned a wrong converted string!!! Infact looking at the byte array of the serialization and the byte array of the deserialization there were a lot of differences!
    The original topic wasn't pointed at this, I know, but with this operation it seemed to me that I could get only one "encoding" of the string and not two.
    Learn, this is the Keyword...

  6. #6
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    I've done this in Java, and then in VB.NET. To avoid problems like this, I would put the hex representation of the bytes in the string instead of what you're doing. While it does effectively double the amount of space the object takes up while in transit, it prevents unusual characters from being present in the string.

    As for your code, if you're going to encode the string in ASCII, you should also decode it in ASCII. I would change this line:
    Dim sw As New IO.StreamWriter(ms)

    ...to this one:
    Dim sw As New IO.StreamWriter(ms, Encoding.ASCII)

    The problem, like I said, is that you could get unusual/unexpected characters, such as null characters, for example.

    Here is the code that I used to encode/decode the byte array to/from a string. At the time, I just wrote it real qucik, so it's possible that it could be more efficiently coded.
    VB Code:
    1. 'Conversion to/from hex string
    2.     Private Shared Function ByteArrayToHexString(ByVal ByteArray() As Byte) As String
    3.         Dim B As Byte
    4.         Dim sHex As String
    5.         Dim StrOut As New System.Text.StringBuilder()
    6.         For Each B In ByteArray
    7.             sHex = Hex(B)
    8.             If sHex.Length = 1 Then
    9.                 sHex = "0" & sHex
    10.             End If
    11.             StrOut.Append(sHex)
    12.         Next
    13.         Return StrOut.ToString
    14.     End Function
    15.     Private Shared Function HexStringToByteArray(ByVal Str As String) As Byte()
    16.         Dim Bytes As New System.Collections.ArrayList()
    17.         Dim B As Byte
    18.         Dim I As Integer
    19.         For I = 0 To Str.Length - 1 Step 2
    20.             Bytes.Add(CType(CInt("&H" & Str.Substring(I, 2)), Byte))
    21.         Next
    22.         Return Bytes.ToArray(B.GetType)
    23.     End Function

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325
    I was wandering if I really need to translate the dataset to a string... Because I'm using sockets to implement network functionality, i saw that socket.send method needs a byte array... and serialization just does this... I didn't try yet, I'll do it now. I'll let you know.

    Thx Tygur.
    Learn, this is the Keyword...

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