A
A
arkonst2017-01-13 12:35:06
Programming
arkonst, 2017-01-13 12:35:06

How can I convert a 3 byte number to a 2 byte number?

There is iron, whose address is its serial number consisting of 3 bytes. But I need to somehow convert this 3 byte address, perhaps using some kind of mathematical encoding into 2 bytes. Is it even possible? If before, then how?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
G
Godless, 2017-01-13
@arkonst

In general, the answer to your question is no, you can't. whatever one may say, out of 3 bytes 2 bytes cannot be made lossless.
Otherwise, someone would have already made an archiver for up to 2 bytes of anything ...
But in the order of alternative sex in the head, let's think.

  1. If you write all the bits of a 3-byte number into a string, you will get a sequence of zeros and ones. You can try to compress this line for example zlib.compress in memory, cut off the standard two-byte header and see what happens. If it fits into 2 bytes, then yes. The reverse conversion is as follows - add the zlib header, do zlib.decompress and convert to bytes.
    Tydyshschsch, and if the compressed string does not fit, then nothing will work.
    PS: the compression topic can be promoted by inventing multi-pass schemes, like first the initial bit, then the difference bit with the next one, and then compression to this line, immediately compressing the Huffman bits with some thread of a popular table (ready because you have nowhere to transfer the table); try arithmetic encoding to bits with length.
    But all this is space nonsense - find a place for a byte. ;-)
    It seems to be about you =) : A tale of one byte
    Another link

S
Spetros, 2017-01-13
@Spetros

If this is a lab about entropy, then yes, you can.
And if this is a real task, where the same software will check this serial number, then, no, you can’t.

R
Rsa97, 2017-01-13
@Rsa97

Impossible

X
x67, 2017-01-13
@x67

do you want to lose information?
If not, then the number must be less than 2^16 (unsigned) or have some kind of pattern, for example, from 3 to 12 bits are always zeros.
In this case, for a number less than 2^16, you simply read the low two bytes and get the result. If the number is signed, then it is more difficult - its modulus must be less than 2 ^ 15, and the first bit of the high byte of the old number will also need to be copied into the first bit of the high byte.
If there is some pattern, you find a law according to which you can restore three from two bytes and implement it.
There are still cases when you may not need high precision, then you can divide the number by 2 ^ 8, that is, 256 and you will get a 2-byte number, which, with an error of +128 -127, will carry information about the previous number.
write in more detail, what kind of hardware is this and what is the address? Perhaps in the serials there is always a constant part, or something with the address can be invented.

R
res2001, 2017-01-13
@res2001

You can convert it by cutting off the extra byte, but apparently this method is not very suitable for you.
And for other methods, you need more information - what programming language, what does "the address is its serial number" mean, what kind of address do you have, how will you use these 2 bytes later?

A
arkonst, 2017-01-13
@arkonst

Initially, we decided that the serial number would be the address of the device. This is convenient when we do not have bindings to the poppy address. The device is simple and the serial number is unique for each device. It needs 2 bytes because modbus standard doesn't support 3 byte address entry...it needs 2 bytes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question