Conversion of an array of integers to string In order to save memory space. Strings inside an embedded device are stored in a particular way. Each 32-bit location holds up to 4 chars instead of only 1 char in the usual case.

I wrote a function that convert an encoded string back to its original string. I am new to Python, so I would like to know whether or not I can simplify my code.

The string is encoded in such way that I need to reverse the order of each group of 4 chars.  

#!/usr/bin/python
def arrayOfInt2string(array):
    s = ''
    for a in array:
        a = "%0.8x" % a
        b = list()
        for i in range(0, len(a), 2):
            b.append(chr(int(a[i:i+2], 16)))            
        s = s + ''.join(b.reverse())
    return s

r = (
    0x33323130,
    0x37363534,
    0x00000038,
    0x00000000
)
arrayOfInt2string(r)   


The output is:

"012345678"
If you are going to be playing with string representations of native types, you probably want to take a look at the struct standard library package.

With it, you can quickly process your r as follows:

&gt;&gt;&gt; import struct
&gt;&gt;&gt; ''.join(struct.pack('i', rr) for rr in r)
'012345678\x00\x00\x00\x00\x00\x00\x00'


And you can play with the ordering of the characters by explicitly describing the endianess of your input:

&gt;&gt;&gt; ''.join(struct.pack('&gt;i', rr) for rr in r)  # big endian
'32107654\x00\x00\x008\x00\x00\x00\x00'