Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Byte() -> stream or string

Posted on 2004-04-14
4
2,734 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Alert on Server memory 2 37
SSIS On fail action 5 37
Using data from certificate to re-create certificate programmatically 3 25
Easy filter aspnet 2 24
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

791 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