Answer the question
In order to leave comments, you need to log in
How to convert 1 byte and 0.5 bytes to 1.5 bytes (More details in the description)?
There is a need to store the year, for example, the current one is 2016. This is done as follows. The number is stored in 16 calculus - in this case it is 7E0. Further, 1 byte is allocated for E0, 0.5 bytes are allocated for 7. And all this is written to the structure, and then to the file of the required format. So that's the problem, I read this data into variables, and accordingly I see them in 10 representation - 7 and 224. How do I get my date? I guess I need to allocate 2 bytes and write them there somehow.
Answer the question
In order to leave comments, you need to log in
First, a byte is indivisible, you are talking nonsense.
Secondly, the number is not stored in hex, but in binary form:
2016 == 0b111_11100000
first = 0b111 == 7
second = 224 == 0b11100000
Actually, you need to shift first to the left by 8 and add second to this.
Here addition is equivalent to "logical or":int year = (first << 8) | second;
google bit fields. And also google about the alignment of data structures.
I suppose you need to allocate 2 bytes and write them there somehow.
In addition to structures, there are also unions (union)
#pragma pack(push,1)
union{
int value;
struct {
unsigned char b1;
unsigned char b2;
unsigned char b3;
unsigned char b4;
}
};
#pragma pack(pop)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question