Hello.
I'm porting some code from C# to Qt4 and I met some problems.
The program opens a binary file and retrieves some data from it.
This is code in C#:
string scriptName = args[0];
Encoding jis = Encoding.GetEncoding(932);
using(BinaryReader script = new BinaryReader(scriptFile.OpenRead(), jis))
{
script.BaseStream.Seek(0x020, SeekOrigin.Begin);
uint opcode = script.ReadUInt32();
uint codeSize = script.ReadUInt32();
script.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] scriptBuffer = script.ReadBytes((int)codeSize);
for(int dwptr = 0; dwptr < codeSize; dwptr += 4)
{
uint currentDw = scriptBuffer[dwptr];
if(currentDw != 0x03 && currentDw != 0x07F)
continue;
dwptr += 4;
currentDw = ToUInt32(scriptBuffer, dwptr);
}
}
string scriptName = args[0];
Encoding jis = Encoding.GetEncoding(932);
using(BinaryReader script = new BinaryReader(scriptFile.OpenRead(), jis))
{
script.BaseStream.Seek(0x020, SeekOrigin.Begin);
uint opcode = script.ReadUInt32();
uint codeSize = script.ReadUInt32();
script.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] scriptBuffer = script.ReadBytes((int)codeSize);
for(int dwptr = 0; dwptr < codeSize; dwptr += 4)
{
uint currentDw = scriptBuffer[dwptr];
if(currentDw != 0x03 && currentDw != 0x07F)
continue;
dwptr += 4;
currentDw = ToUInt32(scriptBuffer, dwptr);
}
}
To copy to clipboard, switch view to plain text mode
and reimplemented ToUInt32 (in C#):
static uint ToUInt32(byte[] array, int beginOfs)
{
return(uint)((array[beginOfs+3] << 24) | (array[beginOfs+2] << 16) | (array[beginOfs+1] << 8) | (array[beginOfs]));
}
static uint ToUInt32(byte[] array, int beginOfs)
{
return(uint)((array[beginOfs+3] << 24) | (array[beginOfs+2] << 16) | (array[beginOfs+1] << 8) | (array[beginOfs]));
}
To copy to clipboard, switch view to plain text mode
I ported this code to:
QFile cScript
(fPath
+"/"+fName
);
cScript.seek(0x020);
quint32 opcode;
quint32 codeSize;
cData >> opcode;
cData >> codeSize;
cScript.seek(0);
for(quint32 dwptr = 0; dwptr < codeSize; dwptr += 4)
{
quint32 curr = fragment[dwptr];
if(curr != 0x03 && curr != 0x07F)
continue;
dwptr +=4;
curr = toUint32(fragment, dwptr);
}
QFile cScript(fPath+"/"+fName);
cScript.open(QIODevice::ReadOnly);
QDataStream cData(&cScript);
cData.setByteOrder(QDataStream::LittleEndian);
cScript.seek(0x020);
quint32 opcode;
quint32 codeSize;
cData >> opcode;
cData >> codeSize;
cScript.seek(0);
QByteArray fragment = cScript.read(codeSize);
for(quint32 dwptr = 0; dwptr < codeSize; dwptr += 4)
{
quint32 curr = fragment[dwptr];
if(curr != 0x03 && curr != 0x07F)
continue;
dwptr +=4;
curr = toUint32(fragment, dwptr);
}
To copy to clipboard, switch view to plain text mode
and toUint32 function:
quint32 EustiaTools
::toUint32(QByteArray array, uint offStart
){
return(quint32)((array[offStart+3] << 24) | (array[offStart+2] << 16) | (array[offStart+1] << 8) | (array[offStart]));
}
quint32 EustiaTools::toUint32(QByteArray array, uint offStart)
{
return(quint32)((array[offStart+3] << 24) | (array[offStart+2] << 16) | (array[offStart+1] << 8) | (array[offStart]));
}
To copy to clipboard, switch view to plain text mode
My problem probably lies in this line:
quint32 curr = fragment[dwptr];
quint32 curr = fragment[dwptr];
To copy to clipboard, switch view to plain text mode
And in function toUint32 too.
I checked and my curr before for loop equals 4294967272 when currentDw from C# equals 232.
I changed type of curr from quint32 to int and then curr equals -24.
How to repair that?
Sorry for my bad english.
Thanks in advance.
Bookmarks