By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,698 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,698 IT Pros & Developers. It's quick & easy.

Concise way to format list/array to custom(hex) string

P: n/a
Hello,

What will be a concise & efficient way to convert a list/array.array of
n elements into a hex string? For e.g. given the bytes
[116, 111, 110, 103, 107, 97]
I would like the formatted string
0x74 0x6f 0x6e 0x67 0x6b 0x61

Is there an approach better than below:
hex = ''
for b in bytes:
hex += ('0x%x '%b)
Thanks
Kurien

Test program:
import array
import sys

bytes = array.array('B')
bytes.fromstring("tongka")
print bytes
hex = ''
for b in bytes:
hex += ('0x%x '%b)
print hex

Aug 2 '08 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Aug 2, 9:53 pm, Kurien Mathew <kmat...@envivio.frwrote:
Hello,

What will be a concise & efficient way to convert a list/array.array of
n elements into a hex string? For e.g. given the bytes
[116, 111, 110, 103, 107, 97]
I would like the formatted string
0x74 0x6f 0x6e 0x67 0x6b 0x61

Is there an approach better than below:
hex = ''
for b in bytes:
hex += ('0x%x '%b)
hex = ' '.join('0x%02x' % b for b in bytes)

Aug 2 '08 #2

P: n/a
Kurien Mathew írta:
Hello,

What will be a concise & efficient way to convert a list/array.array of
n elements into a hex string? For e.g. given the bytes
[116, 111, 110, 103, 107, 97]
I would like the formatted string
0x74 0x6f 0x6e 0x67 0x6b 0x61

Is there an approach better than below:
hex = ''
for b in bytes:
hex += ('0x%x '%b)
<snip>

You should avoid multiple string additions, as each one creates a new
string object (str objects are immutable). Try this:

bytes = [116, 111, 110, 103, 107, 97]
string = ''.join( ['0x%x '%b for b in bytes] )
Aug 2 '08 #3

P: n/a
On 2008-08-02, Zoltán Nagy <ab*****@gmail.comwrote:
Kurien Mathew írta:
>Hello,

What will be a concise & efficient way to convert a list/array.array of
n elements into a hex string? For e.g. given the bytes
[116, 111, 110, 103, 107, 97]
I would like the formatted string
0x74 0x6f 0x6e 0x67 0x6b 0x61

Is there an approach better than below:
hex = ''
for b in bytes:
hex += ('0x%x '%b)
<snip>

You should avoid multiple string additions, as each one creates a new
string object (str objects are immutable). Try this:

bytes = [116, 111, 110, 103, 107, 97]
string = ''.join( ['0x%x '%b for b in bytes] )
That results in an extra ' ' and the end of the string. Try
this:

string = ' '.join(['0x%02x' % b for b in bytes])

--
Grant Edwards grante Yow! ... Just enough
at time to do my LIBERACE
visi.com impression...
Aug 2 '08 #4

P: n/a
On Aug 2, 9:29*am, Grant Edwards <gra...@visi.comwrote:
On 2008-08-02, Zoltán Nagy <abes...@gmail.comwrote:


Kurien Mathew írta:
Hello,
What will be a concise & efficient way to convert a list/array.array of
n elements into a hex string? For e.g. given the bytes
[116, 111, 110, 103, 107, 97]
I would like the formatted string
0x74 0x6f 0x6e 0x67 0x6b 0x61
Is there an approach better than below:
hex = ''
for b in bytes:
* * hex += ('0x%x '%b)
<snip>
You should avoid multiple string additions, as each one creates a new
string object (str objects are immutable). Try this:
bytes = [116, 111, 110, 103, 107, 97]
string = ''.join( ['0x%x '%b for b in bytes] )

That results in an extra ' ' and the end of the string. *Try
this:

string = ' '.join(['0x%02x' % b for b in bytes])

--
Grant Edwards * * * * * * * * * grante * * * * * * Yow! *... Just enough
* * * * * * * * * * * * * * * * * at * * * * * * * time to do my LIBERACE
* * * * * * * * * * * * * * * *visi.com * * * * * *impression...
There is also a built-in hex() method in the Python Standard Library.
So, to make it cleaner:
' '.join(hex(x) for x in bytes)
Aug 2 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.