파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬(Phthon): int 클래스 - from_bytes 메서드(method)


/// 설명

주어진 바이트 배열에 대한 정수를 반환합니다.

※ 형식
classmethod int.from_bytes(bytes, byteorder, *, signed=False)

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# b'\x00d', b'\x03\xe8', b"'\x10"
# 'big' byteorder
# the most significant byte is at the beginning of the byte array.
print(int.from_bytes(b'\x00d', byteorder='big'))  # 100
print(int.from_bytes(b'\x03\xe8', byteorder='big'))  # 1000
print(int.from_bytes(b"'\x10", byteorder='big'))  # 10000
print(int.from_bytes(b'\x27\x10', byteorder='big'))  # 10000
print('-' * 20)
# 'little' byteorder
# the most significant byte is at the end of the byte array.
print(int.from_bytes(b'\x00d', byteorder='little'))  # 25600
print(int.from_bytes(b'\x03\xe8', byteorder='little'))  # 59395
print(int.from_bytes(b"'\x10", byteorder='little'))  # 4135
print(int.from_bytes(b'\x27\x10', byteorder='little'))  # 4135
print('-' * 20)
 
# -10000(2'complement) -> b'\xd8\xf0'
print(int.from_bytes(b'\xd8\xf0', byteorder='big', signed=True))
# 55536's array of bytes -> b'\xd8\xf0'
print(int.from_bytes(b'\xd8\xf0', byteorder='big', signed=False))
print('-' * 20)
 
# using a list
print(int.from_bytes([100], byteorder='big'))  # 65536
print('-' * 20)
 
# List(array of bytes)
test = bytes([1])
print(test)  # b'\x01'
test = bytes([10])
print(test)  # b'\x01\x00'
test = bytes([100])
print(test)  # b'\x01\x00\x00'
test = bytes([1000])
print(test)  # b'\x01\x00\x00\x00'
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수