How to append two bytes in python?

2024/9/8 10:33:56

Say you have b'\x04' and b'\x00' how can you combine them as b'\x0400'?

Answer

Using python 3:

>>> a = b'\x04'
>>> b = b'\x00'
>>> a+b
b'\x04\x00'
https://en.xdnf.cn/q/73026.html

Related Q&A

Pythonic way to write a function which modifies a list?

In python function arguments are passed by object reference. This means the simplest code to modify a list will modify the object itself.a = [1,2,3]def remove_one(b):b.remove(1)remove_one(a) print(a)T…

Trying different functions until one does not throw an exception

I have some functions which try various methods to solve a problem based on a set of input data. If the problem cannot be solved by that method then the function will throw an exception.I need to try t…

Python: Extracting lists from list with module or regular expression

Im trying to extract lists/sublists from one bigger integer-list with Python2.7 by using start- and end-patterns. I would like to do it with a function, but I cant find a library, algorithm or a regula…

Converting hard integral to lambda function with lambdify

I would like to lambdify the function Integral(t**t,(t,0,x)). It works, but my new function, which was returned by lambdify, doesnt return a number but only sympy.integrals.integrals.Integral class. Bu…

python topN max heap, use heapq or self implement?

theres heapq in python, for general usage. i want recording topN(0~20) for 10e7 records.if use heapq, should use - to translate max to min; and recording a min number of bottom, to call heapq.heappushp…

QSortFilterProxyModel returning artificial row

Im using a QSortFilterProxyModel to filter results from a QAbstractListModel. However, Id like to return a first entry which is not present in the original model, that is, its somehow artificial.This i…

@login_required is losing the current specified language

I am using i18n_patterns to internationalize my app and its working except when I click on a link that requires login (a view protected by @login_required decorator), I am being redirected to the login…

Python slow on fetchone, hangs on fetchall

Im writing a script to SELECT query a database and parse through ~33,000 records. Unfortunately Im running into problems at the cursor.fetchone()/cursor.fetchall() phase of things.I first tried iterati…

Pure Python Quadtree Implementation

All,There are a few examples on implementing a quadtree using Python but my question is, does anyone know of a class written in pure python as in a single .py file that I can easily include in my proje…

AttributeError: tuple object has no attribute write

I have a homework assignment for a Python class and am running into an error that I dont understand. Running Python IDLE v3.2.2 on Windows 7.Below is where the problem is happening:#local variables num…