[Python] [Ruby] hex string(16진수 문자열)의 endian을 큰 endian에서 작은 단위로 변경합니다.


생각


Hexstring은 두 글자로 한 바이트를 표시하기 때문에 단순히 문자열의 배열을 반대 순서로 하면 서로 다른 바이트열이 된다.str에서 bytes로 바뀐 후 반대 순서대로 순조롭게 진행될 수 있습니다.

메서드


Python


방법1

import binascii

hex_be = 'f0148c'
bytes_be = binascii.unhexlify(hex_be)
bytes_le = bytes_be[::-1]
hex_le = binascii.hexlify(bytes_le).decode()
hex_le  # '8c14f0'

방법2


4@shiracamus선생님, 감사합니다.
hex_be = 'f0148c'
bytes_be = bytes.fromhex(hex_be)
bytes_le = bytes_be[::-1]
hex_le = bytes_le.hex()
hex_le  # '8c14f0'

Ruby

hex_be = 'f0148c'
bytes_be = Array(hex_be).pack('H*')
bytes_le = bytes_be.reverse
hex_le = bytes_le.unpack1('H*')
hex_le # '8c14f0'

좋은 웹페이지 즐겨찾기