Solved

Byte() -> stream or string

Posted on 2004-04-14
4
2,726 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:monosodiumg
4 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10821838
0
 
LVL 6

Accepted Solution

by:
DaniPro earned 250 total points
ID: 10821870
I think that this is what you need:

C#
byte[] myArray = new byte[] {50, 70, 71};
String s2 = System.Text.Encoding.ASCII.GetString(myArray);
MessageBox.Show(s2);

VB
Dim myArray As Byte() = {50, 70, 71}
Dim s2 As String
s2 = System.Text.Encoding.ASCII.GetString(myArray)
MessageBox.Show(s2)

0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10822904
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
0
 
LVL 9

Expert Comment

by:malharone
ID: 10825232
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
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question