'Converting Python struct.pack to C#
I'm converting some python code to C# and was successful so far, but there's this one part that I don't understand and is arguably the most important part as it involves file decompression and turning into JSON and human readable format.
file_data: bytes = file.read() 'I understand this
file_data: bytes = struct.pack('B' * len(file_data), *file_data[::-1]) 'This is the part that I do not understand, everything after the 'B'
file_data: bytes = zlib.decompress(file_data) 'should be fine, c# should have same library
file_data: tuple = pickle.loads(file_data, encoding='windows-1251')
Solution 1:[1]
That python code line simply reverses the code and removes the header.
Here's the c# code that does the same.
Hashtable UnpickledGP;
byte[] FileBytes = File.ReadAllBytes("GameParams.data").Reverse().ToArray();
byte[] ModdedFileBytes = new byte[FileBytes.Length - 2]; //remove file header
Array.Copy(FileBytes, 2, ModdedFileBytes, 0, ModdedFileBytes.Length);
using (Stream StreamTemp = new MemoryStream(ModdedFileBytes))
{
using (MemoryStream MemoryStreamTemp = new MemoryStream())
{
using (DeflateStream DeflateStreamTemp = new DeflateStream(StreamTemp, CompressionMode.Decompress))
{
DeflateStreamTemp.CopyTo(MemoryStreamTemp);
}
ModdedFileBytes = MemoryStreamTemp.ToArray();
}
}
using (Unpickler UnpicklerTemp = new Unpickler())
{
object[] UnpickledObjectTemp = (object[])UnpicklerTemp.loads(ModdedFileBytes);
UnpickledGP = (Hashtable)UnpickledObjectTemp[0];
UnpicklerTemp.close();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CruleD |