C# 数学、块和条目

C# 数学、块和条目,c#,math,block,C#,Math,Block,我有一个块的数量总是236字节。然后我有一些条目需要放入块中,这些条目有时不能完全放入块中,所以块中会留下一些空字节 我正在努力找出写条目的位置以及应该在哪个块中 int blocklen = 236;//always the same int entryindex = 14;//example index of an entry to write int entrylength = 16;//this will be the same in a

我有一个块的数量总是236字节。然后我有一些条目需要放入块中,这些条目有时不能完全放入块中,所以块中会留下一些空字节

我正在努力找出写条目的位置以及应该在哪个块中

        int blocklen = 236;//always the same

        int entryindex = 14;//example index of an entry to write
        int entrylength = 16;//this will be the same in all entries in these blocks.
        int blockindex = ((entryindex + 1) * entrylength) / blocklen;//this works and will correctly calculate the index of the block to write to.
        int offset = ((entryindex) * entrylength) % blocklen;// this is incorrect, I need it to work out the offset with in the block.
如果我的entryindex是13,那么它将在块0中计算为@208,这是正确的。但是如果它是14,它就不适合第一个块,所以它应该是块1@0,但是它说块1在偏移量224,224是第一个块中的偏移量,但我需要将它带到下一个块中


反正我的数学不太好,今天又不是我的好日子,所以我想知道你们中是否有人能帮我写这行代码。

你们的blocklen不是16的偶数倍

14*16=224

因此,偏移量将为:

int entries_per_block = 14;

int offset = (entryindex * entrylength) % (entrylength * entries_per_block);
块索引应为:

int blockindex = (entryindex * entrylength) / (entrylength * entries_per_block);

你为什么要把这个问题标记为C和C?我在想C和C的编码器都能帮我解决这个问题。如果这是错误的,很抱歉。如果语言不相关,我建议使用伪代码并询问伪代码答案。C和C#的区别太大了,会引起混淆。对不起,我把标签去掉了。我只使用C#编写代码,但我假设除了指针之外,基本算术都是一样的。但是,如果它们的差异非常大,那就错了。谢谢,我知道块长度不是倍数,所以每个块中都会有空空间。实际上,我正在反转其他人的二进制文件,而这不是我存储数据的方式。当它允许我时,我会将你标记为已回答。