Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Bytes to String. 1

Status
Not open for further replies.

tshad

Programmer
Joined
Jul 15, 2004
Messages
386
Location
US
I am reading a field as Binary and want to use as string function on it.

How do I convert it to string? If I try to use CStr and get an error.

Code:
Dim arrByteData As Byte()
while (objDataReader.Read() = true)
	if(objDataReader(0) is System.DBNull.value) then
		exit while
	else
		arrByteData = Ctype(objDataReader(0),Byte())
		response.BinaryWrite(arrByteData)
	end if
end while

How do I do a search on this. I am trying to find the word "SUBJECT:" and then get all the text up to 2 tabs. If I read it into a string I can use the following:

Code:
dim indexStart as Integer
dim indexEnd as Integer
dim indexCount as Integer
dim theString as String
		
indexStart = strContents.IndexOf("SUBJECT:")
indexEnd = strContents.IndexOf(chr(7) & chr(7),indexStart)
theString = mid(strContents,strContents.IndexOf("SUBJECT:")+10,indexCount)
		
indexCount = (indexEnd - indexStart) - 10
response.write("string is = " & theString & "<br><br>")

But I need to convert it to a string to do it. Is there a better way to work on the data as a Byte array (since I already have it that way)? Cstr(arrByteData) gives me an error saying I can't convert Bytes to String.

Thanks,

Tom.
 
Put "Encoding Base Types" into Index.

Dim AE As New System.Text.ASCIIEncoding()
Dim ByteArray As Byte() = { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116, 114, 105, 110, 103, 46 }
'Remarks
' Any element of the bytes array that is greater than
' hexadecimal 0x7F is translated to a Unicode question mark ('?').

Dim s As String = AE.GetString(ByteArray)


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Did the job.

I assume this is moving the Byte array into another object.

Why did I have to use System.Text.ASCIIEncoding? Couldn't I just moved it using string and the GetString function?

Thanks,

Tom.
 
It happens to be a word document.

Thanks,

Tom.
 
Uhhh, o-kay.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Gawd, some of your posts are like soooo helpful Chip ;)

lol

-----------------------------------------------
"The night sky over the planet Krikkit is the least interesting sight in the entire universe."
-Hitch Hiker's Guide To The Galaxy
 
I guess I'm amazed that someone would read a MS-Word document as if it's a text file. But there's probably a good reason...

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I'm not.

I am trying to read it and display it on one of my other web pages as a Word document, but having a problem getting it to work

In this case, I have moved all my word documents (about 150 of them) into my Sql Server. I need to read each record and do a search for the word "SUBJECT:" followed by 2 tabs. Between this is the subject of the page. This will be put into the Sql record.

This will allow my people to have access to our QA documents on line. I will be able to use this to display all the titles on my page that the user can choose. This will have the title of the document to read.

Tom.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top