I have the following piece of code which converts an array of bytes to a unicode string and back to a byte array. However for certain byte values they don't map correctly?
Dim testArr() As Byte = {108, 84, 177, 173, 60, 36, 200, 240, 63, 218, 156, 141, 119, 61, 100, 232, 38, 222, 26, 148, 133, 70, 226, 252, 209, 79, 202, 208, 62, 197, 223, 4}
Dim encUni As New System.Text.UnicodeEncoding
dim vecStr as string = encUni.GetString(majorTestArra)
Response.Write("vector string: " & vecStr & "<br />")
Response.Write("vector length: " & bytIV.Length & "<br />")
Dim vecB() As Byte= encUni.GetBytes(vecStr)
For Each item As Byte In vecB
Response.Write(item.ToString & ", ")
Next
The result i get is 108, 84, 177, 173, 60, 36, 200, 240, 253, 255, 156, 141, 119, 61, 100, 232, 253, 255, 26, 148, 133, 70, 226, 252, 209, 79, 202, 208, 62, 197, 223, 4
As you can see the following values map incorrectly. Below is the stuffed items (original, new value)
rupurt
Member
145 Points
38 Posts
Weird result converting byte array to unicode and back
Aug 13, 2008 08:13 AM|LINK
I have the following piece of code which converts an array of bytes to a unicode string and back to a byte array. However for certain byte values they don't map correctly?
Dim testArr() As Byte = {108, 84, 177, 173, 60, 36, 200, 240, 63, 218, 156, 141, 119, 61, 100, 232, 38, 222, 26, 148, 133, 70, 226, 252, 209, 79, 202, 208, 62, 197, 223, 4} Dim encUni As New System.Text.UnicodeEncoding dim vecStr as string = encUni.GetString(majorTestArra) Response.Write("vector string: " & vecStr & "<br />") Response.Write("vector length: " & bytIV.Length & "<br />") Dim vecB() As Byte= encUni.GetBytes(vecStr) For Each item As Byte In vecB Response.Write(item.ToString & ", ") NextCodelines
Member
2 Points
1 Post
Re: Weird result converting byte array to unicode and back
Feb 14, 2013 11:28 AM|LINK
I was with a similar problem and changed my code to use:
byte[] bts = UnicodeEncoding.Default.GetBytes(textToEncrypt);
and:
string txt = UnicodeEncoding.Default.GetString(bytesToDecrypt);