1

I just have a quick question about how to get the length of a file containing hexadecimal numbers. For example:

724627916C 

The only way I can think is to convert hex value to binary:

724627916C => 0111001001000110001001111001000101101100 

then count the bits of binary value. Just wondering if this is the correct way? Thanks

5
  • 1
    Duplicate: stackoverflow.com/questions/238603/… Commented Nov 3, 2009 at 19:44
  • 2
    Maybe you want to know the parity of a file, not its length? Commented Nov 3, 2009 at 19:59
  • 1
    @jheddings: the question title is a duplicate - but the question doesn't really match the title. @unknown: what are you asking about; it is not at all clear. Commented Nov 4, 2009 at 1:50
  • Thanks, i just realized now, actually, i have to handle two types of file Type 1 : File contains Hex values. Type 2 : File contains a sequence of characters. and for the Type 1 file, i need to convert Hex values to characters, then calculate the length of converted sequence of characters rather than simply getting the length of the file. And i do not need to change anything on type 2 file. Because of the confusion of these two file types, i keep getting the unexpected result. Anyway, problem had been solved. Thanks. Commented Nov 5, 2009 at 19:25
  • Possible duplicate of How do you determine the size of a file in C? Commented May 2, 2018 at 2:05

5 Answers 5

16

No, this is not the correct way.

FILE *fp = fopen("filename", "rb"); fseek(fp, 0, SEEK_END); int lengthOfFile = ftell(fp); fclose(fp); 

In principle: Open a file, seek to the end and retrieve your current position. What you'll get is the number of bytes in the file.

Edit: Your question is a little unclear. Do you want to retrieve the number of bytes in the file, or the number of bytes represented by the hexadecimal string in the file? If the latter is the case, and you don't have any whitespaces in your file, just divide the number of bytes returned by the method above by 2.

Sign up to request clarification or add additional context in comments.

2 Comments

THanks, Actually, i need to retrieve the number of bytes(or bits) in the file, including whitespaces.
1) fseek() spec.: --> "A binary stream need not meaningfully support fseek calls with a whence value of SEEK_END." 2) ftell() returns a long.
2

In un*x-like operating systems, stat can be used for that purpose.

Comments

1

Try this answer for some help. If that doesn't work, Google has plenty of information on determining file size in C.

Comments

0

If you want to count the bits, it would be simpler to set up an array that tells you how many bits are set in each hex digit (indexed by character code), and then add things up without explicitly converting to binary.

Assuming C99:

static bits_map[256] = { ['0'] = 0, ['1'] = 1, ['2'] = 1, ['3'] = 2, ['4'] = 1, ['5'] = 2, ['6'] = 2, ['7'] = 3, ['8'] = 1, ['9'] = 2, ['a'] = 2, ['b'] = 3, ['c'] = 2, ['d'] = 3, ['e'] = 3, ['f'] = 4, ['A'] = 2, ['B'] = 3, ['C'] = 2, ['D'] = 3, ['E'] = 3, ['F'] = 4, }; size_t n_bits = 0; int c; while ((c = getchar()) != EOF) n_bits += bits_map[c]; printf("Number of bits set: %zd\n", n_bits); 

Comments

0

File length in C goes use the function _filelength()

#include <io.h> int fn, fileSize; FILE * f = fopen(&String, "rb"); if (f) { fn = _fileno(f); fileSize = _filelength(fn); } fclose(f); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.