[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Color representation as rgb, int and hex

On Mon, Sep 9, 2019 at 6:01 AM Eko palypse <ekopalypse at> wrote:
> I'm confused about the following
> import sys
> print(tuple(bytes.fromhex('282C34')))
> print(tuple((0x282C34).to_bytes(3, byteorder=sys.byteorder)))
> which results in
> (40, 44, 52)
> (52, 44, 40)
> on my machine. Shouldn't I expect the same result?

Your first example is a sequence of three bytes: 28 in the first
position, then 2C, then 34 in the last position.

Your second example has 28 in the most-significant byte, 2C in the
middle, and 34 in the least-significant byte.

For those to come out identical, the concepts of "most-significant
byte" and "first position" have to mean the same, which means you want
big-endian, which is also referred to as "network byte order". So
don't use sys.byteorder - just explicitly ask for big-endianness:

print(tuple((0x282C34).to_bytes(3, "big")))
(40, 44, 52)