Tôi đã không thực hiện bất kỳ điểm chuẩn nào, nhưng công thức này "hoạt động cho tôi".
Phiên bản ngắn: Sử dụng ____10, sau đó là kết quả. Tuy nhiên, ma quỷ nằm trong các chi tiết, vì
class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
1 yêu cầu một số chữ số Hex chẵn, mà class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
3 không đảm bảo. Xem DocString, và các bình luận nội tuyến tự do để biết chi tiết.from binascii import unhexlify
def long_to_bytes [val, endianness='big']:
"""
Use :ref:`string formatting` and :func:`~binascii.unhexlify` to
convert ``val``, a :func:`long`, to a byte :func:`str`.
:param long val: The value to pack
:param str endianness: The endianness of the result. ``'big'`` for
big-endian, ``'little'`` for little-endian.
If you want byte- and word-ordering to differ, you're on your own.
Using :ref:`string formatting` lets us use Python's C innards.
"""
# one [1] hex digit per four [4] bits
width = val.bit_length[]
# unhexlify wants an even multiple of eight [8] bits, but we don't
# want more digits than we need [hence the ternary-ish 'or']
width += 8 - [[width % 8] or 8]
# format width specifier: four [4] bits per hex digit
fmt = '%%0%dx' % [width // 4]
# prepend zero [0] to the width, to zero-pad the output
s = unhexlify[fmt % val]
if endianness == 'little':
# see //stackoverflow.com/a/931095/309233
s = s[::-1]
return s
... và các bài kiểm tra đơn vị nosetest của tôi ;-]
class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
Có bao nhiêu byte là một int trong Python?
Phương thức
class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
4 trả về một đối tượng bytearray là một mảng của các byte được cho.Thí dụ
prime_numbers = [2, 3, 5, 7]
# convert list to bytearray
byte_array = bytearray[prime_numbers]
print[byte_array]
# Output: bytearray[b'\x02\x03\x05\x07']
Bytearray [] cú pháp
Cú pháp của phương pháp
class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
4 là:bytearray[[source[, encoding[, errors]]]]
Phương thức
class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
4 Trả về một đối tượng bytearray [tức là mảng byte] có thể thay đổi chuỗi số nguyên trong phạm vi class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
7.Nếu bạn muốn phiên bản bất biến, hãy sử dụng phương thức byte [].
tham số bytearray []
class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
4 lấy ba tham số tùy chọn:- Nguồn [Tùy chọn] - Nguồn để khởi tạo mảng byte. - source to initialize the array of bytes.
- Mã hóa [tùy chọn] - Nếu nguồn là một chuỗi, mã hóa chuỗi. - if the source is a string, the encoding of the string.
- Lỗi [tùy chọn] - Nếu nguồn là một chuỗi, hành động cần thực hiện khi chuyển đổi mã hóa không thành công [đọc thêm: mã hóa chuỗi] - if the source is a string, the action to take when the encoding conversion fails [Read more: String encoding]
Tham số nguồn có thể được sử dụng để khởi tạo mảng byte theo các cách sau:source parameter can be used to initialize the byte array in the following ways:
Sợi dây | Chuyển đổi chuỗi thành byte bằng cách sử dụng 9 cũng phải cung cấp mã hóa và lỗi tùy chọnencoding and optionally errors
|
Số nguyên | Tạo một mảng có kích thước được cung cấp, tất cả được khởi tạo thành null |
Sự vật | Bộ đệm chỉ đọc của đối tượng sẽ được sử dụng để khởi tạo mảng byte |
Có thể lặp lại | Tạo một mảng có kích thước bằng với số lượng ITEBLE và được khởi tạo thành các phần tử có thể lặp lại phải có số nguyên của các số nguyên giữa 7 |
Không có nguồn [đối số] | Tạo một mảng có kích thước 0. |
bytearray [] giá trị trả về
Phương thức
class TestHelpers [object]:
def test_long_to_bytes_big_endian_small_even [self]:
s = long_to_bytes[0x42]
assert s == '\x42'
s = long_to_bytes[0xFF]
assert s == '\xff'
def test_long_to_bytes_big_endian_small_odd [self]:
s = long_to_bytes[0x1FF]
assert s == '\x01\xff'
s = long_to_bytes[0x201FF]
assert s == '\x02\x01\xff'
def test_long_to_bytes_big_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567]
assert s == '\xab\x23\x45\x6c\x89\x01\x23\x45\x67'
def test_long_to_bytes_big_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567]
assert s == '\x01\x23\x45\x67\x89\x01\x23\x45\x67'
def test_long_to_bytes_little_endian_small_even [self]:
s = long_to_bytes[0x42, 'little']
assert s == '\x42'
s = long_to_bytes[0xFF, 'little']
assert s == '\xff'
def test_long_to_bytes_little_endian_small_odd [self]:
s = long_to_bytes[0x1FF, 'little']
assert s == '\xff\x01'
s = long_to_bytes[0x201FF, 'little']
assert s == '\xff\x01\x02'
def test_long_to_bytes_little_endian_large_even [self]:
s = long_to_bytes[0xab23456c8901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x6c\x45\x23\xab'
def test_long_to_bytes_little_endian_large_odd [self]:
s = long_to_bytes[0x12345678901234567, 'little']
assert s == '\x67\x45\x23\x01\x89\x67\x45\x23\x01'
4 trả về một mảng byte có kích thước và giá trị khởi tạo đã cho.Ví dụ 1: Mảng byte từ chuỗi
string = "Python is interesting."
# string with encoding 'utf-8'
arr = bytearray[string, 'utf-8']
print[arr]
Đầu ra
bytearray[b'Python is interesting.']
Ví dụ 2: Mảng byte có kích thước số nguyên
size = 5
arr = bytearray[size]
print[arr]
Đầu ra
bytearray[b'\x00\x00\x00\x00\x00']
Ví dụ 2: Mảng byte có kích thước số nguyên
rList = [1, 2, 3, 4, 5]
arr = bytearray[rList]
print[arr]
Đầu ra
bytearray[b'\x01\x02\x03\x04\x05']
Làm thế nào để bạn tạo một mảng byte trong Python?
String = "Python rất thú vị." # Chuỗi có mã hóa 'UTF-8' ARR = byteArray [Chuỗi, 'UTF-8'] in [ARR] Chạy mã ..
Kích thước = 5. mảng = bytearray [kích thước] in [mảng] mã chạy ..
rlist = [1, 2, 3, 4, 5] mảng = bytearray [rlist] in [mảng] mã chạy ..
Làm thế nào để bạn chuyển đổi int sang giá trị byte?
Phương thức bytevalue số nguyên Java [] Phương thức ByteValue [] của lớp số nguyên của gói java.lang chuyển đổi số nguyên đã cho thành byte sau khi chuyển đổi nguyên thủy thu hẹp và trả về nó [giá trị của đối tượng số nguyên như một byte].Ngoài ra, hãy nhớ phương thức này không ghi đè phương thức byteValue [] của lớp số.The byteValue[] method of Integer class of java. lang package converts the given Integer into a byte after a narrowing primitive conversion and returns it [value of integer object as a byte]. Also, remember this method does override byteValue[] method of the Number class.
Làm thế nào để bạn chọn một byte trong Python?
Chúng ta có thể sử dụng lớp byte tích hợp trong Python để chuyển đổi chuỗi thành byte: chỉ cần chuyển chuỗi làm đầu vào đầu tiên của hàm tạo của lớp byte và sau đó chuyển mã hóa làm đối số thứ hai.In đối tượng hiển thị một biểu diễn văn bản thân thiện với người dùng, nhưng dữ liệu có trong đó là trong byte.pass the string as the first input of the constructor of the Bytes class and then pass the encoding as the second argument. Printing the object shows a user-friendly textual representation, but the data contained in it is in bytes.
Có bao nhiêu byte là một int trong Python?
Để an toàn, Python phân bổ một số byte cố định của không gian trong bộ nhớ cho mỗi biến của một loại số nguyên thông thường, được gọi là int in python.Thông thường, một số nguyên chiếm bốn byte, hoặc 32 bit.four bytes, or 32 bits.