BinaryReader to BinaryStream

I have a .net program that I converted to VB.NET and am wondering how to produce in Xojo.

Code

'Read PostEffect Fog Dim br As New BinaryReader() br.BaseStream(File.OpenRead(TheFile)) Dim theValue As [String] = Nothing For i As Integer = &H3 To &H5 theValue = br.ReadByte().ToString("X2") Next MessageBox.Show(theValue) br.close()

I am unsure on how to reproduce the BinaryReader as a BinaryStream. and output the read value as “000000”.

Thanks,
Shane.

That code basically shows one byte from position 5 despite reading bytes 3, 4 and 5 from the file
This should be close

Dim br As New BinaryStream.Open(File.OpenRead(TheFile))
br.position = &h5
Dim theValue As String = Hex( br.ReadInt8() )
MessageBox.Show(theValue)
br.close()

[quote=38148:@Norman Palardy]That code basically shows one byte from position 5 despite reading bytes 3, 4 and 5 from the file
This should be close

Dim br As New BinaryStream.Open(File.OpenRead(TheFile))
br.position = &h5
Dim theValue As String = Hex( br.ReadInt8() )
MessageBox.Show(theValue)
br.close()[/quote]

Nope. It does work, but it only reads one value. I want multiple values read, such as (“000000”)

I have also tried:

  Dim Fog As New FolderItem
  Fog = App.TheFileName
  
  Dim RFog As BinaryStream = BinaryStream.Open(App.TheFileName, True)
  RFog.LittleEndian = False
  
  For i As Integer = &H3 To &H5
    RFog.Position = i
    ColorValue1 = Hex(RFog.ReadUInt8())
  Next
  MsgBox ColorValue1

And I still only get one hex value returned. How can I read multiple bytes?

FYI: MessageBox doesn’t exist in Xojo. For Xojo, it’s MsgBox or MessageDialog.

The other code only reported one value too so …

Is there a “i++” for Xojo. In C#, you could do for (int i = 0x03; i < 0x05; i++)

How would I do the i++ with Xojo?

Found out how to do it: Different Int values, like ReadInt32. The higher the number, the more is read.

Still not sure WHY it matters since

For i As Integer = &H3 To &H5 theValue = br.ReadByte().ToString("X2") Next
This READS 3 bytes (at most) but only holds on to one of them
theValue is always set to the last value read

[quote=38197:@Norman Palardy]Still not sure WHY it matters since

For i As Integer = &H3 To &H5 theValue = br.ReadByte().ToString("X2") Next
This READS 3 bytes (at most) but only holds on to one of them
theValue is always set to the last value read[/quote]

I got my code working. However, the hex values read are wrong.

  Dim Fog As New FolderItem
  Fog = App.TheFileName
  
  Dim RFog As BinaryStream = BinaryStream.Open(App.TheFileName, True)
  RFog.LittleEndian = False
  
  For i As Integer = &H3 To &H5
    ColorValue1 = Hex(RFog.ReadInt32())
  Next
  EffectViewer.AddRow ColorValue1, "No Comments"

The file: (Top most row are the “offsets(h)”)

00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 00 00 04 00 00 00 00 48 26 4C 00 EC FF FF FF

I’m trying to read offsets 03 to 05, however, my code reads 08 to 0A and two extra bytes. (Output is “48264C00”, when it is supposed to be “040000”) Any suggestions?

The initial reader read BYTES - 8 bits
You’re reading Int32’s which are 4 bytes each

BinaryStream is so broken :frowning:

I tried the same code in C#

private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            String v1 = null;
            var br = new BinaryReader(File.OpenRead(openFileDialog1.FileName));
            for (int i = 0xD; i <= 0xF; i++)
            {
                br.BaseStream.Position = i;
                v1 += br.ReadByte().ToString("X2");
            }
            textBox1.Text = v1;
            br.Close();
        }

And it reads all the bytes I need it to read… Looks like I’ll be using C#. You know what’s weird too? I can code way faster in Visual Studio than I can Xojo.

Shane, it is very difficult not to get rude.

BinaryStream is not broken, but you apparently do not understand the difference between a byte (8 bits) and Int32 (32 bits). Also it appears that you have not catched how a loop works yet. You are overwriting the value each time you iterate…

[quote=38209:@Natascha Chrobok]Shane, it is very difficult not to get rude.

BinaryStream is not broken, but you apparently do not understand the difference between a byte (8 bits) and Int32 (32 bits). Also it appears that you have not catched how a loop works yet. You are overwriting the value each time you iterate…[/quote]

I just wanna know why I can take the same code, convert it manually to C# and have it work perfectly.

Because your code is wrong.

ReadInt32 reads four bytes at a time (as said above), and each time it is called the position it reads from (the offset) increases, by four.

So the third time you call ReadInt32 you are reading bytes 08 to 0B, as expected (the position is 0 based).

I am replying just not to let this thread end with your last sentence. Not that you paid much attention to what other people answered before…

Julen

[quote=38209:@Natascha Chrobok]Shane, it is very difficult not to get rude.

BinaryStream is not broken, but you apparently do not understand the difference between a byte (8 bits) and Int32 (32 bits). Also it appears that you have not catched how a loop works yet. You are overwriting the value each time you iterate…[/quote]

Yes I do understand the difference. Int8 reads a single byte, and int32 reads 6 bytes total. I do also understand how a loop works. How do you think I have the code working correctly in C#? That is what bugs me, is that a simple thing I can do in C# I cannot do in Xojo, and the Xojo language is supposed to be the “easiest cross platform language”? No downers to the IDE, but when it comes to the language, I feel C# is easier, and enables more productive coding.

Actually ReadInt32 reads 12 bytes - 4 each time starting at &h3, 4 at &h4, 4 at &h5 - so they even overlap slightly

The initial code you posted grabs one byte at a time - so you should grab ONE Uint8 at a time in Xojo
It then displays ONE message with the last value in theValue despite having read 3 bytes
Which is what you said the code I initially posted does
If what you want is 3 bytes to display one message then this is a trivial change to the code I gave you initially

  dim theFile as Folderitem = GetOpenFolderItem("")
  if theFile is nil then return
  Dim br As BinaryStream = BinaryStream.Open(TheFile)
  Dim theValue As String
  For i As Integer = &H3 To &H5
    br.position = i
    dim tmp as string = "0" + Hex( br.ReadInt8() ) // make sure we craft this so its 2 characters for each byte
    theValue = theValue + right( tmp, 2 ) // append what we have so far
  next
  br.close()
  MsgBox theValue

However based on what I know about VB.Net this isn’t what your initial code did

I created a small hex file with the sample you posted earlier and at least on OS X this does indeed read byte &h3, &h4 &h5 and return 040000 as the string shown in the message box

Since this just reads BYTES you dont really have to worry about setting endiannes
If you read Uint16’s or Int32’s you might

[quote=38416:@Norman Palardy]Actually ReadInt32 reads 12 bytes - 4 each time starting at &h3, 4 at &h4, 4 at &h5 - so they even overlap slightly

The initial code you posted grabs one byte at a time - so you should grab ONE Uint8 at a time in Xojo
It then displays ONE message with the last value in theValue despite having read 3 bytes
Which is what you said the code I initially posted does
If what you want is 3 bytes to display one message then this is a trivial change to the code I gave you initially

  dim theFile as Folderitem = GetOpenFolderItem("")
  if theFile is nil then return
  Dim br As BinaryStream = BinaryStream.Open(TheFile)
  Dim theValue As String
  For i As Integer = &H3 To &H5
    br.position = i
    dim tmp as string = "0" + Hex( br.ReadInt8() ) // make sure we craft this so its 2 characters for each byte
    theValue = theValue + right( tmp, 2 ) // append what we have so far
  next
  br.close()
  MsgBox theValue

However based on what I know about VB.Net this isn’t what your initial code did

I created a small hex file with the sample you posted earlier and at least on OS X this does indeed read byte &h3, &h4 &h5 and return 040000 as the string shown in the message box

Since this just reads BYTES you dont really have to worry about setting endiannes
If you read Uint16’s or Int32’s you might[/quote]

IT WORKS :slight_smile:

Thanks for the help. From seeing your code, I now know what to do. I’m trying to create cross-platform editors for these file formats

I’m starting with effect files as they’re probably the easiest files to decode.

Once again, THANK YOU, and sorry if my previous posts sounded rude; I was just used to coding with the BinaryReader instead of Xojo’s BinaryStream.