Search

Unicode & Byte Object

[- Disclaimer -] 아래 내용은 정보보안 공부 목적으로 작성된 것이나, 이를 토대로 허가되지 않은 대상에 실습을 진행할 경우 해킹 시도로 간주하여 법적 처벌을 받을 수 있음을 알려 드립니다.
Unicode & Byte Object
✦ 모든 문자열은 Unicode로 처리하고 Unicode로 Encoding되지 않은 모든 문자열은 Byte 객체로 처리
>>> msg = b"hello" >>> msg[0] == 'h' False >>> msg[0] 104 >>>
Plain Text
복사
✦ 이건 뭐 숫자로 출력 되지도 않네
>>> msg = b"hello" >>> for c in msg: ... print(c + ' ', end=' ') ... Traceback (most recent call last): File "<python-input-4>", line 2, in <module> print(c + ' ', end=' ') ~~^~~~~ TypeError: unsupported operand type(s) for +: 'int' and 'str' >>>
Plain Text
복사
✦ chr()을 통해 Byte 객체 문자를 Unicode로 인식될 수 있게 변환
>>> msg = b"hello" >>> for c in msg: ... print(chr(c) + ' ', end='') ... h e l l o >>>
Plain Text
복사
✦ decode()를 통해 Byte 객체 문자를 Unicode로 인식될 수 있게 변환
>>> msg = b"hello" >>> msg = msg.decode() >>> msg[0] == 'h' True >>> msg[0] 'h' >>>
Plain Text
복사