TTMMM's blog

By TTMMM, history, 3 years ago, In English

Can someone please convert this short piece of C code to C# code.

int main(int argc) {
    
    FILE* f = fopen("xyz", "rb");
    uint32_t offset = 0;
    fseek(f, 0x3c, SEEK_SET);
    fread(&offset, sizeof(offset), 1, f);
    fseek(f, offset + 4, SEEK_SET);
    uint16_t machine = 0;
    fread(&machine, sizeof(machine), 1, f);
    printf("Machine: 0x%.4x\n", machine);
}

Thanks in advance!

Tags c++, c#, file
  • Vote: I like it
  • -9
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it +9 Vote: I do not like it

Here u go buddy

<i>
int main(int argc) {
    
    FILE* f = fopen("xyz", "rb");
    uint32_t offset = 0;
    fseek(f, 0x3c, SEEK_SET);
    fread(&offset, sizeof(offset), 1, f);
    fseek(f, offset + 4, SEEK_SET);
    uint16_t machine = 0;
    fread(&machine, sizeof(machine), 1, f);
    printf("Machine: 0x%.4x\n", machine);
</i>
»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Something like this I presume:

int Main(string[] args) {
    using (FileStream fs = File.OpenRead("xyz")) {
        fs.Seek(0x3f, SeekOrigin.Begin);
        byte[] bytes = new byte[4];
        fs.Read(bytes, 0, 4);
        UInt32 offset = BitConverter.ToUInt32(bytes, 0);
        fs.Seek(offset + 4, SeekOrigin.Begin);
        fs.Read(bytes, 0, 2);
        Console.WriteLine("Machine: 0x{0:X4}", BitConverter.ToUInt16(bytes, 0));
    }
    return 0;
}