read back in Binaryreader

S

Supra

i saved file in binary. i saved 16 colours.. but how do i read back
values into listbox?

Dim st2 As Stream = File.Open _
("C:\Documents and Settings\My Documents\Visual Studio
Projects\VbNet\project\Write_Ini_File\bin\colours.dat", FileMode.Open,
FileAccess.Read)
Dim br2 As New BinaryReader(st2)
Do Until br2.PeekChar = -1
ListBox1.Items.Add(br2.ReadByte) ==>this is not what i wanted
...i wanted hex values (colour) into listbox
Loop
br2.Close()
st2.Close()
 
M

Morten Wennevik

Hi Supra,

How you read the colors back depends on how you wrote them in the first place.
If the colors are in in hex RGB format (3 bytes) you could do something like
(C# code)

byte[] rgb = br2.ReadBytes(3);
ListBox1.Items.Add(rgb[0].ToString("X") + rgb[1].ToString("X") + rgb[2].ToString("X"));

Of course, PeekChar wouldn't work since you read three bytes.
 
S

Supra

i will explain to u. but i have done in vb6 b4.
in mirc chat when user clicked colour and click Okbutton to save file
in binary. but how do i read back values(limited 6 only) and also how
do i write back when saving? i had 16 colours in file.
regards
 
M

Morten Wennevik

I'm sorry Supra, but I'm not familiar with how mirc saves the file.
However if you have done it in VB6 you should be able to do it in VB.Net.

The BinaryReader.ReadByte reads a byte. It may be that this byte is all that is necessary to store color information for mirc but you need to convert the byte into some other value. Byte.ToString("X") will convert the byte to a hexadecimal string value. So

ListBox1.Items.Add(br2.ReadByte().ToString("X"))

should give you the hex value of a single byte. However I believe you need all three bytes to get the full color.

If you give us the VB6 code for the color reading it would be easier for us to help you.
 
M

Morten Wennevik

Hi Supra,

How you read the colors back depends on how you wrote them in the first place.
If the colors are in in hex RGB format (3 bytes) you could do something like
(C# code)

byte[] rgb = br2.ReadBytes(3);
ListBox1.Items.Add(rgb[0].ToString("X") + rgb[1].ToString("X") + rgb[2].ToString("X"));

Of course, PeekChar wouldn't work since you read three bytes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top