#String을 bytes로 변환
myStr = "Hello"
str_utf = myStr.encode('utf-8')
str_utf2 = bytes(myStr, 'utf-8')
#bytes를 String으로 변환
myBytes = b'Hello'
myStr = myBytes.decode('utf-8')
myStr2 = str(myBytes, 'utf-8')
import binascii
#bytes를 16진수 형태로 출력
Bytes = b"Hello"
Bytes_hex = Bytes.hex() 또는 Bytes_hex = binascii.hexlify(bytes).decode('utf-8')
print(Bytes_hex)
문자열을 16진수로 변환
import binascii
#String을 Bytes로 변환
str_val = 'Hello'.encode('utf-8')
#Bytes를 16진수 형태로 변환 return값 str ex) '485219c4'
hex_val = binascii.hexlify(str_val).decode('utf-8') 또는 hex_val = str_val.hex()
print(hex_val)
hex_val = '0x' + hex_val
n_hex = int(hex_val, 16)
print(hex(n_hex))
#utf-8로 인코딩된걸로 하기 싫으면 utf-8부분을 다른 걸로 바꿔주면 됨
16진수를 String으로
n = 0x4120717569636b2062726f776e20666f78
#string식으로 바꾸고 앞에 0x삭제
n_str = hex(n)[2:]
print(n_str)
#bytes형식으로 바꾸기
n_bytes = bytes.fromhex(n_str)
print(n_bytes)
#bytes를 문자열로
n_string = str(n_bytes, 'utf-8') 또는 n_str = n_bytes.decode('utf-8')
print(n_string)
근데 binascii는 특정한 상황이 아니면 사용하지 않을 것 같다 ex)utf-8이 아닐때