Hướng dẫn how do you convert bytes to numbers in python? - làm thế nào để bạn chuyển đổi byte thành số trong python?

Tôi hiện đang làm việc trên một chương trình mã hóa/giải mã và tôi cần có khả năng chuyển đổi byte thành một số nguyên. Tôi biết điều đó:

bytes[[3]] = b'\x03'

Tuy nhiên, tôi không thể tìm hiểu làm thế nào để làm nghịch đảo. Tôi đang làm gì sai lầm khủng khiếp?

Ggorlen

37K7 Huy hiệu vàng61 Huy hiệu bạc77 Huy hiệu đồng7 gold badges61 silver badges77 bronze badges

Đã hỏi ngày 30 tháng 11 năm 2015 lúc 23:04Nov 30, 2015 at 23:04

4

Giả sử bạn trên ít nhất là 3.2, có một bản tích hợp cho việc này:

________ 8 [bytes,

## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
0, *,
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
1]
[ bytes,
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
0, *,
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
1 ]

...

Đối số bytes phải là một đối tượng giống như byte hoặc một byte sản xuất có thể lặp lại.

Đối số

## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
0 xác định thứ tự byte được sử dụng để biểu thị số nguyên. Nếu
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
0 là
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
5, byte quan trọng nhất là ở đầu mảng byte. Nếu
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
0 là
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
7, byte quan trọng nhất là ở cuối mảng byte. Để yêu cầu thứ tự byte gốc của hệ thống máy chủ, hãy sử dụng
## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
8 làm giá trị đơn hàng byte.

Đối số

## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024
9 cho biết liệu hai bổ sung có được sử dụng để đại diện cho số nguyên hay không.

## Examples:
int.from_bytes[b'\x00\x01', "big"]                         # 1
int.from_bytes[b'\x00\x01', "little"]                      # 256

int.from_bytes[b'\x00\x10', byteorder='little']            # 4096
int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]  #-1024

Martineau

Huy hiệu vàng 115K2525 gold badges160 silver badges284 bronze badges

Đã trả lời ngày 30 tháng 11 năm 2015 lúc 23:08Nov 30, 2015 at 23:08

Peter DeGlopperPeter DeglopperPeter DeGlopper

35.3k7 Huy hiệu vàng89 Huy hiệu bạc83 Huy hiệu đồng7 gold badges89 silver badges83 bronze badges

4

Danh sách các byte có thể đăng ký [ít nhất là trong Python 3.6]. Bằng cách này, bạn có thể lấy lại giá trị thập phân của từng byte.

>>> intlist = [64, 4, 26, 163, 255]
>>> bytelist = bytes[intlist]       # b'@\x04\x1a\xa3\xff'

>>> for b in bytelist:
...    print[b]                     # 64  4  26  163  255

>>> [b for b in bytelist]           # [64, 4, 26, 163, 255]

>>> bytelist[2]                     # 26 

Đã trả lời ngày 7 tháng 5 năm 2020 lúc 17:16May 7, 2020 at 17:16

0

int.from_bytes[ bytes, byteorder, *, signed=False ]

Không hoạt động với tôi Tôi đã sử dụng chức năng từ trang web này, nó hoạt động tốt

//coderwall.com/p/x6xtxq/convert-bytes-to-int-or-int-to-bytes-in-python

def bytes_to_int[bytes]:
    result = 0
    for b in bytes:
        result = result * 256 + int[b]
    return result

def int_to_bytes[value, length]:
    result = []
    for i in range[0, length]:
        result.append[value >> [i * 8] & 0xff]
    result.reverse[]
    return result

Đã trả lời ngày 9 tháng 11 năm 2018 lúc 21:12Nov 9, 2018 at 21:12

1

>>> intlist = [64, 4, 26, 163, 255]
>>> bytelist = bytes[intlist]       # b'@\x04\x1a\xa3\xff'

>>> for b in bytelist:
...    print[b]                     # 64  4  26  163  255

>>> [b for b in bytelist]           # [64, 4, 26, 163, 255]

>>> bytelist[2]                     # 26 
0 có thể được sử dụng để chuyển đổi byte thành int [hoạt động trong Python 3.7]:

list[b'\x03\x04\x05']
[3, 4, 5]

Đã trả lời ngày 22 tháng 4 lúc 7:58Apr 22 at 7:58

3

Trong trường hợp làm việc với dữ liệu được đệm, tôi thấy điều này hữu ích:

int.from_bytes[[buf[0],buf[1],buf[2],buf[3]], "big"]

Giả sử rằng tất cả các yếu tố trong

>>> intlist = [64, 4, 26, 163, 255]
>>> bytelist = bytes[intlist]       # b'@\x04\x1a\xa3\xff'

>>> for b in bytelist:
...    print[b]                     # 64  4  26  163  255

>>> [b for b in bytelist]           # [64, 4, 26, 163, 255]

>>> bytelist[2]                     # 26 
1 đều dài 8 bit.

Đã trả lời ngày 1 tháng 6 năm 2021 lúc 8:23Jun 1, 2021 at 8:23

Một câu hỏi cũ mà tôi tình cờ tìm thấy một giải pháp hiện có. Cuộn của riêng tôi và nghĩ rằng tôi sẽ chia sẻ vì nó cho phép bạn tạo số nguyên 32 bit từ danh sách các byte, chỉ định một bù.

def bytes_to_int[bList, offset]:
    r = 0
    for i in range[4]:
        d = 32 - [[i + 1] * 8]
        r += bList[offset + i] 

Bài Viết Liên Quan

Chủ Đề