How to Append Two Bytes in Python

Combining two bytes in Python

Left-shift the second byte by 8 bits and bitwise OR it with the first byte:

(second_byte << 8) | first_byte

For extra safety clamp both bytes to [0, 255] with a bitwise AND:

((second_byte & 0xFF) << 8) | (first_byte & 0xFF)

Python: join two bytearray objects

Create a new combined bytearray from two:

byt_combined = byt1 + byt2

Extend one bytearray with another. This changes byt1:

byt1.extend(byt2)

How to append to bytes in python 3

bytes is immutable. Use bytearray.

xs = bytearray(b'\x01\x02\x03')
xs.append(5)

Python bytes concatenation

Bytes don't work quite like strings. When you index with a single value (rather than a slice), you get an integer, rather than a length-one bytes instance. In your case, a[0] is 20 (hex 0x14).

A similar issue happens with the bytes constructor. If you pass a single integer in as the argument (rather than an iterable), you get a bytes instance that consists of that many null bytes ("\x00"). This explains why bytes(a[0]) gives you twenty null bytes. The version with the curly brackets works because it creates a set (which is iterable).

To do what you want, I suggest slicing a[0:1] rather than indexing with a single value. This will give you a bytes instance that you can concatenate onto your existing value.

a += a[0:1]

What is the best way to merge 2 byte arrays?

Chunk taken from here: How do you split a list into evenly sized chunks?

Merge taken from here: How do I merge two lists into a single list?

Combined into this:

def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]

a1 = [10,20,30,40,50,60,70,80]
b1 = [11,21,31,41,51,61,71,81]
combined = [j for i in zip(chunks(a1, 2),chunks(b1, 2)) for j in i]
out = bytes(bytearray([x for pair in combined for x in pair]))
==> b'\n\x14\x0b\x15\x1e(\x1f)2<3=FPGQ'

Python Concatenate Bytes

Indexing into bytes directly gives an int, so the easiest solution here is to just make data_bs into bytes and use slices:

In [132]: data_bs = b''
...: c = b'\x00\x00\xa0\xe1\x00'
...:
...: for i in list(range(0, len(c), 2)):
...: data_bs += c[i+1:i+2] + c[i:i+1]
...:

In [133]: data_bs
Out[133]: b'\x00\x00\xe1\xa0\x00'

Concatenate / Split two bytes using unique character

Since images do not contain commas, I am concatenating the bytes using , character, but when I try to split the bytes back, it returns multiple chunks of bytes.

Image file might generally contain any byte, including , (\x2C). If you wish to use concatenating and splitting you have to process image so it does never contain certain byte. You might do it using built-in python module base64. base64.b64encode function accept bytes and return bytes encoded using base64 algorithm which contain solely bytes listed in Table 1: The Base 64 Alphabet of RFC 3548, so you might simply join such-coded images using any character not present in said table. After receiving split using character you have chosen and then feed elements into base64.b64decode function to get original bytes. Keep in mind that this method will increase size of send messages.



Related Topics



Leave a reply



Submit