Hello,
How to read the data properly in a Memoryblock to show ‘00 00 00 0D’ hex as ‘13’ decimal?
When trying to read a small PNG file, the IDE reads the binary stream of the PNG file correctly, and places the data in a MemoryBlock correctly. The correct data is shown below when a breakpoint at ‘Dim x as Integer’ is created.
Figure 1. Correct format and data order of PNG file.
When the first 8 bytes of PNGHeader.PNGSignature are read and shown with EncodeHex, the data is in the correct order, which is ‘89 50 4E 47 0D 0A 1A 0A’.
When trying to view the next 4 bytes ’00 00 00 0D’ the value should be decimal 13 or hex D. The value instead shows as decimal 218103808, or hex 00 D0 00 00.
Here is the Xojo program, and the 2x2.png file that can be downloaded: Download PNG1.zip
What am I missing to show the correct value of hex 00 00 00 0D?
Structure PNGHeader
PNGSignature as String * 8
IHDRLengthBytes as String * 4 // not CString
End Structure
Var m As MemoryBlock = PNGStruct.IHDRLengthBytes
m.LittleEndian = False
Var n As Uint32 = m.UInt32Value(0) // Stored as BigEndian comes 13
It seems like a second memoryblock is needed to reverse the mixed numbers of the original memoryblock.
As an example the 00 00 00 0D is in the original memoryblock correctly according to the breakpoint as a string. When trying to view a UInteger32, the number is reversed. Adding another memoryblock then reverses the reversal (does that make sense?).
Your first memory block is using Little Endianess as default byte ordering, the struct 00 00 00 0D for 13 is clearly Big Endian (Big first, little last) so I just created a second one to keep whatever you are doing untouched, and read the value informing Xojo to consider a Big Endian layout for such 4 bytes in memory.