Long int to byte array converting

Hello, colleagues!
Please give me advice,
I have gote a unix time from DS3231 module like a value 5CE3B975(HEX) or 1558428021(DEC).
I need a create byte array from this value like {0x5C,0xE3,0xB9,0x75}.

How i can make it?

Thanks in advance.

I have gote a unix time from DS3231 module

How did you do that?

I need a create byte array from this value

Why?

SHTIRLITZ:
Hello, colleagues!
Please give me advice,
I have gote a unix time from DS3231 module like a value 5CE3B975(HEX) or 1558428021(DEC).
I need a create byte array from this value like {0x5C,0xE3,0xB9,0x75}.

How i can make it?

Thanks in advance.

TIP: google 'union in c'

union {
unsigned long val;
uint8_t bytes[4];
} long_num;

long_num.val = 0x5CE3B975 //now long_num.bytes[0] = 0x5C, and so on for bytes[1..3]

sherzaad:
union {
unsigned long val;
uint8_t bytes[4];
} long_num;

long_num.val = 0x5CE3B975 //now long_num.bytes[0] = 0x5C, and so on for bytes[1..3]

If it's an UNO, Nano... (ATMega) 'unsigned long' is Little Endian.

So long_num.bytes[0] = 0x75, long_num.bytes[1] = 0xB9, and so on.

leongjerland:
If it's an UNO, Nano... (ATMega) 'unsigned long' is Little Endian.

So long_num.bytes[0] = 0x75, long_num.bytes[1] = 0xB9, and so on.

well caught... always get them mixed up! :slight_smile:

anyway, the principle holds...

sherzaad:
TIP: google 'union in c'

union {
unsigned long val;
uint8_t bytes[4];
} long_num;

long_num.val = 0x5CE3B975 //now long_num.bytes[0] = 0x5C, and so on for bytes[1..3]

Thanks a lot, it's working!