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'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值