A
A
Alexander2016-02-24 19:56:02
C++ / C#
Alexander, 2016-02-24 19:56:02

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

4 answer(s)
D
Denis Zagaevsky, 2016-02-24
@poter

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;

S
Sergey, 2016-02-24
Protko @Fesor

google bit fields. And also google about the alignment of data structures.

S
Stanislav Makarov, 2016-02-24
@Nipheris

I suppose you need to allocate 2 bytes and write them there somehow.

You need to count these two bytes as a whole (for example, if you are using potiki, then you need to count in short int), or, if this is not possible, then count the bytes separately and combine them into int16_t, as Denis Zagayevskiy advises . In both cases, you need to understand what the byte order in a number is. When you understand what byte order is, you will understand what the essence of your question is. You need to follow him, because. in a file, this byte order can be one (and it is determined by the file format), but on your machine, in theory, it may be different, and you need to understand this when you glue int16_t for you. If you will not glue it, but will read it immediately into int16_t, then you need to check the byte order on your architecture and in the file format.
And yes, numbers in modern calc. technology:
a) are stored in the binary number system - i.e. everything you see in the debugger or in your program depends entirely on how the number-to-string function (which you then see) selects the characters to write the number. In most of these functions, you can specify the number system. Write such a function yourself to understand what it is about;
b) their size in memory/processor registers is aligned to the size of a byte (usually 8 bits). A byte is a quantum of addressing and data processing, so to say that a number takes 0.5 bytes is not entirely correct from a practical point of view, because bytes can only be taken as a whole.
Because about 0.5 bytes you mentioned rather by mistake, I think you don't need bit fields.

N
neosapient, 2016-02-24
@neosapient

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)

now each byte can be accessed by name b1, b2, b3, b4
Otherwise, learn bitwise operations |, &, ^, as well as bitwise shift <<, >>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question