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”.
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”)
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.
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"
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?
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.
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.
[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
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.