Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Python string out of non-ascii "bytes"

I need to create a Python string consisting of non-ascii bytes to be used as a command buffer in a C module. I can do that if I write the string by hand:

mybuffer = "\x00\x00\x10"

But I cannot figure out how to create the string on the fly if I have a set of integers which will become the bytes in the string. Concatenating an integer with a string is a TypeError.

So if I have a list of integers lets say:

myintegers = [1, 2, 3, 10]

How can I convert that into a string "\x01\x02\x03\x0A"

I am using Python 2.6.

like image 547
sarshad Avatar asked Oct 04 '10 12:10

sarshad


People also ask

How do you get non ASCII characters in Python?

In order to use non-ASCII characters, Python requires explicit encoding and decoding of strings into Unicode. In IBM® SPSS® Modeler, Python scripts are assumed to be encoded in UTF-8, which is a standard Unicode encoding that supports non-ASCII characters.

What is \\ x00 -\\ x7F?

US-ASCII is a character set (and an encoding) with some notable features: Values are between 0–127 (x00–x7F) ASCII code-point 32 (decimal) represents a SPACE. ASCII code-point 65 represents the uppercase letter A.


1 Answers

u''.join(map(unichr, myintegers)) will do what you want nicely.

like image 123
JasonFruit Avatar answered Sep 20 '22 13:09

JasonFruit