|
-
May 9th, 2003, 05:19 PM
#1
Thread Starter
Hyperactive Member
Two Encoding decode...
Let's suppose a string. I use
VB Code:
dim abyte() as byte
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...
-
May 10th, 2003, 01:34 PM
#2
Lively Member
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:
Dim mychars() As Char = Encoding.Unicode.GetChars(abyte)
-
May 11th, 2003, 06:17 AM
#3
Thread Starter
Hyperactive Member
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...
-
May 11th, 2003, 07:41 AM
#4
Lively Member
Can you post the code you used to do this?
-
May 11th, 2003, 09:14 AM
#5
Thread Starter
Hyperactive Member
Look at this thread for understanding the problem I had.
http://www.vbforums.com/showthread.p...hreadid=244481
VB Code:
Public Function DataSetToString(ByRef ds As DataSet) As String
Dim ms As MemoryStream = New MemoryStream()
Dim bf As New BinaryFormatter()
bf.Serialize(ms, ds)
ms.Position = 0
Dim sr As Byte()
sr = ms.ToArray()
Return Encoding.ASCII.GetString(sr) 'or unicode, is the same
End Function
Public Function StringToDataSet(ByVal data As String) As DataSet
Dim ms As New IO.MemoryStream()
Dim sw As New IO.StreamWriter(ms)
Dim bf As New BinaryFormatter()
sw.Write(data)
sw.Flush()
ms.Position = 0
Dim ds As New DataSet()
ms.Position = 0
ds = bf.Deserialize(ms)
Return ds
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...
-
May 11th, 2003, 12:04 PM
#6
Lively Member
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:
'Conversion to/from hex string
Private Shared Function ByteArrayToHexString(ByVal ByteArray() As Byte) As String
Dim B As Byte
Dim sHex As String
Dim StrOut As New System.Text.StringBuilder()
For Each B In ByteArray
sHex = Hex(B)
If sHex.Length = 1 Then
sHex = "0" & sHex
End If
StrOut.Append(sHex)
Next
Return StrOut.ToString
End Function
Private Shared Function HexStringToByteArray(ByVal Str As String) As Byte()
Dim Bytes As New System.Collections.ArrayList()
Dim B As Byte
Dim I As Integer
For I = 0 To Str.Length - 1 Step 2
Bytes.Add(CType(CInt("&H" & Str.Substring(I, 2)), Byte))
Next
Return Bytes.ToArray(B.GetType)
End Function
-
May 11th, 2003, 12:23 PM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|