Solved

Byte() -> stream or string

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now