简体   繁体   English

C#-以已知的偏移量从文件获取字节

[英]C# - Get bytes from file at known offset

As a wee fore-note, this is me first proper C# program, and me experience with programming is mostly with making Pascal scripts for TES5Edit. 首先,这是我第一个适合的C#程序,而我的编程经验主要是为TES5Edit编写Pascal脚本。 Made two actual programs in Lazarus but, err, they're pretty terrible. 在Lazarus中编写了两个实际程序,但是,很糟糕。

Uploaded me current code 'ere: http://www.mediafire.com/download/fadr8bc8d6fv7cf/WindowsFormsApplication1.7z 向我上传了当前代码'ere: http : //www.mediafire.com/download/fadr8bc8d6fv7cf/WindowsFormsApplication1.7z

Anyhow! 无论如何! What I'm currently trying to do, is get the byte values at two specific offsets in a .dds file. 我目前正在尝试做的是获取.dds文件中两个特定偏移量的字节值。 The x resolution is kept @ offset +0c, and consists of two bytes (so +0c and +0d). x分辨率保持为@ offset + 0c,并由两个字节组成(即+ 0c和+ 0d)。 Same gig for the y resolution; y分辨率相同的演出; @ offset +10 & +11. @偏移+10和+11。 I uploaded me findings here: http://pastebin.com/isBKwaas 我在这里上传了我的发现: http : //pastebin.com/isBKwaas

I've no clue how to do this, however. 但是,我不知道如何执行此操作。 The most I've been able to decipher from various google results, has resulted with this: 我所能从各种Google搜索结果中得出的最大结论是:

        public void GetDDSDimensions(string strDDSFilename, out int iSourceDDSx, out int iSourceDDSy)
    {
        FileStream fsSourceDDS = new FileStream(strDDSFilename, FileMode.Open, FileAccess.Read);
        int iWidthOffset = 12; // 0c in hex, if byte is 00, then size is => 256, so get +0d as well
        int iHeightOffset = 16; // 10 in hex, same gig as above. Get byte @ +11 if +10 is 00.
        byte[] bufferDDSBytes = new byte[24]; // @ Offset +24 , byte is always 01. Use as a wee, err, "landmark".

    }

No clue how to move on from there. 不知道如何从那里继续前进。 I need to somehow set bufferDDSBytes to nab the first 24 bytes in fsSourceDDS, then compare the hex values @ +0c and +10, in order to get the resolution of the .dds file. 我需要以某种方式设置bufferDDSBytes来获取fsSourceDDS的前24个字节,然后比较十六进制值@ + 0c和+10,以获得.dds文件的分辨率。

Comparing should be easy; 比较应该很容易; C# should have a hex equivalent to Pascal's StrToInt() function, no? C#应该具有与Pascal的StrToInt()函数等效的十六进制,不是吗?

first, use using :-) 首先,使用using :-)

using (FileStream fsSourceDDS = new FileStream(strDDSFilename, FileMode.Open, FileAccess.Read))
{
     // do something with the FileStream
} // Ensures that it is properly closed/disposed

To go to a specific offset in the stream, use the Seek method: 要转到流中的特定偏移量,请使用Seek方法:

fsSourceDDS.Seek(someOffset, SeekOrigin.Begin);

and call ReadByte or Read method on it to get as many bytes as you want. 并对其调用ReadByteRead方法以获取所需的字节数。 After reading bytes, the position in the stream is advanced by the number of bytes read. 读取字节后,流中的位置将增加读取的字节数。 You can get the current position in the stream with the Position property. 您可以使用Position属性获取流中的当前位置。 To read little-endian values directly from the stream, you can use the BinaryReader class. 要直接从流中读取小尾数值,可以使用BinaryReader类。

To combine all of the above: 结合以上所有内容:

using (FileStream fsSourceDDS = new FileStream(strDDSFilename, FileMode.Open, FileAccess.Read))
using (BinaryReader binaryReader = new BinaryReader(fsSourceDDS))
{
    fsSourceDDS.Seek(0x0c, SeekOrigin.Begin);
    ushort with = binaryReader.ReadUInt16();
    fsSourceDDS.Seek(0x10, SeekOrigin.Begin);
    ushort height = binaryReader.ReadUInt16();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM