python基本語法1.1--十進制與十六進制數之間的相互轉換


#大端與小端
print
((1024).to_bytes(2, byteorder = 'big')) print((65536).to_bytes(8, byteorder = 'little'))

#有符號與無符號 print((-1024).to_bytes(4, byteorder = 'big', signed = True))#b'\xff\xff\xfc\x00' print((-1024).to_bytes(4, byteorder = 'little', signed = True))#b'\x00\xfc\xff\xff' #異常現象(把有些數字直接根據ASCII碼表翻譯過來了) print((3124).to_bytes(2, byteorder = 'big')) # why \x0c4 => \x0c + 4(0x34) print((3140).to_bytes(2, byteorder ='little')) # why D\x0c => D(0x44) + 0x0c #把十進制轉換成十六進制 print('%x' % 3345) #d11 print('%x' % 3124) #c34

#把十六進制轉換成十進制 print(0xd11) #3345 print(0xc34) #3124

b = b'china\r\nus' print(type(b)) #<class 'bytes'>
#將其他編碼的字符串轉換成Unicode編碼
s = b.decode() print(s)#china us

#將Unicode編碼轉換成其他進制編碼 print(s.encode())#b'china\r\nus'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM