-
help with memory size
hey..i m having trouble with fixed-length format..suppose i wanna create a struct to store my variables n later on convert it into binary file format from raw text file like below :
/* this only part of the source code. not a full program*/
Code:
struct test {
char a[10];
char b[10];
char c[5];
unsigned int d;
};
how do i made the int d occupy at most 1 byte to be stored in binary format (for eg.if int d is equal to 48 and it must stored in 48(in decimal)
but it must no be stored as the string 48, that occupying 2 bytes. Can anyone give a tips??Thanks
-
If you are sure the integer is <= 255 you can cast it as an unsigned char, but if that's the case then why not just use unsigned char to begin with
-
yea..thanks for reply..but because i need to read in the file and store in struct and later on i will be able to write back to binary file format and if i using unsigned char will the output will be different from unsigned int??
-
But if it's always <= 255, then just leave it as unsigned char.
You can still do normal arithmetic on it if you want to.