monosodiumg
asked on
Byte() -> stream or string
I get HTML as a Byte array from a remote object. I need to get this into a string or stream. I don't know anything about the encoding (ascii, unicode?).
The code that fills the byte array uses a binary reader on a filestream with no encoding specified. The file read by the Filestream can be assumed to be ASCII.
Comments on any aspect of this welcome.
The code that fills the byte array uses a binary reader on a filestream with no encoding specified. The file read by the Filestream can be assumed to be ASCII.
Comments on any aspect of this welcome.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This function will return a string when given a byte array:
Public Function ByteArrayToString(ByVal bytArray() As Byte) As String
Dim UTF8 As New UTF8Encoding()
Return UTF8.GetString(bytArray)
End Function
Public Function ByteArrayToString(ByVal bytArray() As Byte) As String
Dim UTF8 As New UTF8Encoding()
Return UTF8.GetString(bytArray)
End Function
i think the question is if you don't know the original encoding of the byte array .. how do you "normalize" the string.
e.g. say the string is "ABC" in ASCII .. which gets into bytes array .. and you convert to a UNICODE string. The resultant string would be "AxBxCx" (x represents the null character -- since unicode is 2 bytes and ascii is one, it'd insert a null char to fill the gap).
What i do is check wether 1st, 3rd, and 5th bytes are null (assuming 0 bases array index). if they're null then the original byte array is in unicode else it's in ascii.
- malhar
e.g. say the string is "ABC" in ASCII .. which gets into bytes array .. and you convert to a UNICODE string. The resultant string would be "AxBxCx" (x represents the null character -- since unicode is 2 bytes and ascii is one, it'd insert a null char to fill the gap).
What i do is check wether 1st, 3rd, and 5th bytes are null (assuming 0 bases array index). if they're null then the original byte array is in unicode else it's in ascii.
- malhar
http://www.freevbcode.com/ShowCode.asp?ID=209