简体   繁体   中英

read a disk block as bitmap

I am trying to read a block which contains block bitmap and inode bitmap

I read a block as a unsigned char array than I convert it to binary as follows:

for (i = 0; i < 4096; i++) {
    for (j = 8; j <=0 ; j--) {
        bits[j] = bitmap[i]%2;
        bitmap[i] = bitmap[i]/2;
    }
    for(t=0; t<8;t++)
        printf("%d\t",bits[t]);
    printf("\n");
    }

when I put '0' to char and print it as

printf("%d",'0');

I get 48 and my bits array contains 00110000 That works, however when I check inode bitmap it does not work for example a bitmap is:

1 1 1 0 0 0 0
but I get
0 0 0 0 1 1 1

I could not check if same thing happens with block bitmap.

To repeat, the code works normal conversation for example it prints 00110000 which is 48, for char '0' which print 48 also. This swapping occurs with inode bitmap. When I change for it will work for inode bitmap but how can I now it will work for blok bitmap. This will fix the code but the logic is wrong.

Any idea?

The lines

for(t=0; t<8;t++)
        printf("%d\t",bits[t]);

print the bit at position 0 (the least significant one) first and the bit at position 7 (the most significant) last. As you want it to be the other way around, change the loop to:

for(t=7; t>=0;t--)

or similar.

looks like your bit order is swapped big-endian vs little endian. http://en.wikipedia.org/wiki/Endianness you can swap with htonl, htons, ntohl, ntohs family of functions. try man htons . or run your loop in reverse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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