Nelu said:
Is the byte an octet? How is the encoding done? How do you get the values
in hex?
Let's say your int is represented on 2 bytes at 8 octet a byte: b0=0x01,
b1=0x40. This means b0=1 and b1=64. You could have 256*b1+b0, 256*b0+b1
and a lot of other options. 256*b0+64=256+64=320 seems to be what you're
looking for.
For 4 bytes: 0*2^24+0*2^16+1*2^8+154=256+154=410. On 286 machines int is
2 bytes so you'll never be able to pull this off if you have 3 non-null
bytes for an unsigned number.
You have to make sure that the values are saved this way. If you just
write an int and then move the file onto another platform, using the
above formula may give you the wrong results.
You may need to take care of sign issues.
You really need to know exactly how those values have been saved to be
able to reconstruct the information correctly.
Uh, yeah, what he said...
Look, as far a "C" is concerned, there really isn't any difference
between "hex" and "integer" in a "binary file", AS LONG AS the
"binary" hex type and integer type are the same number of bytes
and compiled on the same machine and the same compiler...BUTTTT
if you are talking about "octets", that is a different kettle of fish...
And this is why your question doesn't make a lot of sense...if
you've stored an integer of a certain number of bytes using a
certain machine and a certain compiler, all you have to do is
declare an "int" and store your number in that variable at the
pointer location of the start of the number, something like:
char *char_ptr;
int *number_ptr;
int number;
char *char_ptr get_number(char *char_pointer)
{ whatever }
number_ptr = (int *)get_number(char_ptr);
number = *number_ptr;
Or something like that, that's the basic idea...