Python Forum
hex to int convertion - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: hex to int convertion (/thread-28131.html)



hex to int convertion - ebolisa - Jul-06-2020

Hi,

Why isn't the date printed correctly?

from datetime import datetime as dt

now = dt.now()

print(hex(now.second)) #return 0x0
print(hex(now.minute)) #return 0x16
print(hex(now.hour)) #return 0x15
print(hex(now.weekday())) #return 0x0
print(hex(now.day)) #return 0x6
print(hex(now.month)) #return 0x7
print(hex(now.year-2000)) #returns 0x14

# actual datetime = 2020 7 6 21 22 0 0(weekday)

# sec min hour weekday day month year(2 digit)
t = [0x0, 0x16, 0x15, 0x0, 0x6, 0x7, 0x14]
w = ["Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat"]

print("20%x/%02x/%02x %02x:%02x:%02x %s" %(t[6],t[5],t[4],t[2],t[1],t[0],w[t[3]+1]))
#converting to int no change
print("20%x/%02x/%02x %02x:%02x:%02x %s" %(int(t[6]),int(t[5]),int(t[4]),int(t[2]),int(t[1]),int(t[0]),w[int(t[3])+1]))



RE: hex to int convertion - menator01 - Jul-06-2020

Here is one way to do it
#! /usr/bin/env python3

from datetime import datetime as dt
now = dt.now()
time_arr = [now.year, now.month, now.day, now.hour, now.minute, now.second]
hex_time = []
for item in time_arr:
    hex_time.append(hex(item))
    
print(hex_time)
print(' '.join(hex_time))
Output:
['0x7e4', '0x7', '0x6', '0xf', '0xd', '0x9'] 0x7e4 0x7 0x6 0xf 0xd 0x9



RE: hex to int convertion - ebolisa - Jul-06-2020

Thank you.