2013-02-19 31 views
0

我有大量的块总是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,而是它表示在偏移量224处的块1,并且224是第一个块中的偏移量,但是我需要将它移到下一个块。

反正数学不太好,这不是我的一天,所以我只是想知道你们中的任何一位能否帮助我完成这一行代码。

+3

你为什么要纪念这个问题,C和C#? – 2013-02-19 14:03:26

+0

我在想C和C#编码器都可以帮我修复它。对不起,如果这是错的。 – godzcheater 2013-02-19 14:09:56

+1

如果语言不相关,我建议使用伪代码并要求伪代码答案。 C和C#非常不同,会造成混淆。 – 2013-02-19 14:14:19

回答

2

你的blocklen不是16的倍数!

14 * 16 = 224

所以抵消将是:

int entries_per_block = 14; 

int offset = (entryindex * entrylength) % (entrylength * entries_per_block); 

和blockindex应该是:

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

谢谢,我知道块的长度不是一个整数,所以每个块都会有空的空间。我实际上正在颠倒别人的二进制文件,这不是我将如何存储数据。 当你允许我时,你会回答你的回答。 – godzcheater 2013-02-19 14:14:18