?
Solved

Byte() -> stream or string

Posted on 2004-04-14
4
Medium Priority
?
2,737 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
[X]
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
4 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10821838
0
 
LVL 6

Accepted Solution

by:
DaniPro earned 750 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.   …
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:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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