Important information: this site is currently scheduled to go offline indefinitely by end of the year.

Read String VB.net

Coders and would-be coders alike, this is the place to talk about programming.
Post Reply
Matsy
beginner
Posts: 20
Joined: Mon Aug 21, 2006 8:12 am

Read String VB.net

Post by Matsy »

Hey,
When I was messing around with VB.net with a BinaryReader to read a string, I only saw an option to read a 7 character string, and no others.
Would there be an option to just read a null terminated string?, Because I can't seem to find it.

Cheers,
Matsy
Silver
veteran
Posts: 80
Joined: Sat Apr 30, 2005 1:25 pm
Been thanked: 2 times

Post by Silver »

I think the VB.net Binary String Reader reads 1 byte before the string and uses that for the string length.

I don't think I have even looked I've always just used something like this

dim test as string
dim byter as byte = 1
....
While byter <> 0
byter = bread.ReadByte
test+= Chr(byter)
End While

I'd hope there was something built in, but laziness had always resorted in me using that above. :D
Matsy
beginner
Posts: 20
Joined: Mon Aug 21, 2006 8:12 am

Post by Matsy »

Works perfect, Thanks :D
Rahly
VVIP member
VVIP member
Posts: 411
Joined: Thu Aug 05, 2004 10:17 am
Been thanked: 1 time

Post by Rahly »

also very slow too

i don't use vb.net but c# equiv is


with a FileStream or BinaryReader, i use filestreams

Code: Select all

byte[] bytestring = new byte[size];
fs.read(bytestring);
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string mystring = encoding.GetString(bytestring);
This works out better because its easier to change encodings. For say UTF8 or UTF16LE/BE.

VB.NET example

Code: Select all

Dim dBytes As Byte() = ...
Dim str As String
Dim enc As New System.Text.ASCIIEncoding()
str = enc.GetString(dBytes)
"By nature men are alike. Through practice they have become far apart." Confucius (Analect 17:2)
Post Reply