HI I'm new to this forum I'm having a peculiar problem I'm trying to read a 64 bit data binary file I have 5 integer variables a b c d e the problem is I have to read the 64 bit data like 0-7 bits as integer value to a 8-15 bits as int into b 16-18 bits as int into c etc like that for eg if 0-7 bits are 0011010 then a should be a int value of 26 my format of binary file looks like the following 00000000 070E0000160A04C0 031000000F010280 .......À.......€ 00000010 0A090000131401C0 1E1700001A0601C0 .......À.......À 00000020 1A0B05001E1701C0 18110100031603C0 .......À.......À 00000030 090C0000130001C0 00120700140E03C0 .......À.......À 00000040 18150500010E0380 0B160600160602C0 .......€.......À 00000050 0A0E0000130B01C0 19010100050803C0 .......À.......À 00000060 1B0801000C0F03C0 011607001D1303C0 .......À.......À so in the first line I'm interested only in 070E0000160A04C0 since this is the 64 bit data so for the binary file I set the hex line length as 8 and the view changes to lsb msb 00000000 070E0000160A04C0 .......À 00000008 031000000F010280 .......€ 00000010 0A090000131401C0 .......À 00000018 1E1700001A0601C0 .......À 00000020 1A0B05001E1701C0 .......À 00000028 18110100031603C0 .......À 00000030 090C0000130001C0 .......À Which format you think will be better I'm completely new to reading binary files or binary format itself Can you please let me know how to read specific range of bits to a specific variable A code sample would be of great help Thank you
00000000 070E00001...etc isn't a binary file, it's a textual/hex representation of a binary file. So it doesn't matter in the least whether the representation is 00000000 070E0000160A04C0 031000000F010280 or 00000000 070E0000160A04C0 00000008 031000000F010280 because the data will be exactly the same in both cases. Personally I'd just read the file a byte at a time and manipulate the destination variables so that they contain the right data. It doesn't make sense to talk about a "64-bit binary file" - binary is binary, that's it. It may have been generated by a 64-bit program but that doesn't mean it uses "64-bit binary".
I appreciate your response ok I got you on the file type part But i can you please let me know how to read a binary file atleast byte by byte ill try to figure out a way of assigning specific bits to specific variables I need a way of reading the 30th bit to another value b and 31st bit value to c a sample code that illustrates such functionality would be of great help thanks
Code: // untested! FILE *fp=fopen("filename","rb"); char c=fgetc(fp); You might want to check the return value from fopen before using it, if the file open failed then fp will be NULL. After the second line c should contain the first byte in the file. Use feof() to determine if end of file has been reached (if necessary). c might need to be declared unsigned char instead of char; this might make later code a bit easier. To assign the 30th bit to b, the 30th bit will be in the 5th byte (but will it be bit 2 or bit 5?). When you've read the 5th byte, test the relevant bit with bitwise AND and assign 1 or 0 to b depending on the outcome.
Reading 32bit Packed Binary Data On 64bit System Code: struct { WORD version; BOOL upgrade; time_t time1; time_t time2; } apparms; File *fp; fp = fopen(filePath, "r+b"); fread(&apparms, sizeof(apparms), 1, fp); return Py_BuildValue("{s:i,s:l,s:l}", "sysVersion",apparms.version, "powerFailTime", apparms.time1, "normKitExpDate", apparms.time2 ); Now on a 32 bit system this works great, but on a 64 bit my time_t sizes are different (32bit vs 64 bit longs).
yep, so when reading a file created on a 32-bit system in a 64-bit system, either you have to use different structs to read the data then copy it to a struct apparms for normal processing, or you need to create a separate file conversion utility.