본문 바로가기
파이썬

파이썬 Python-10 진수에서 16 진수로, 역방향 바이트 순서, 16 ​​진수에서 10 진수로

by º기록 2020. 10. 2.
반응형

나는 struct.pack 및 hex 등에 대해 많이 읽었습니다.

10 진수를 2 바이트로 16 진수로 변환하려고합니다. 16 진수 비트 순서를 반대로 한 다음 다시 10 진수로 변환합니다.

이 단계를 따르려고 노력 중입니다 ... 파이썬

Convert the decimal value **36895** to the equivalent 2-byte hexadecimal value:

**0x901F**
Reverse the order of the 2 hexadecimal bytes:

**0x1F90**
Convert the resulting 2-byte hexadecimal value to its decimal equivalent:

**8080**

 

해결 방법

 

>>> x = 36895
>>> ((x << 8) | (x >> 8)) & 0xFFFF
8080
>>> hex(x)
'0x901f'
>>> struct.unpack('<H',struct.pack('>H',x))[0]
8080
>>> hex(8080)
'0x1f90'

 

참조 페이지 https://stackoverflow.com/questions/5995812

 

 

반응형

댓글