473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
4 16050
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Servé Lau | last post by:
suppose I want to use sscanf get the functionname from a function prototype. Is the following format string correct then? char funcname; char *p = "func(void)"; sscanf(p, "%s", funcname);...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
6
by: Larry Lard | last post by:
I am trying to apply the following easy-to-state formatting rule to display a number: - If the number is positive or negative, display it in the 'default' way - If the number is zero, the...
3
by: gregcm | last post by:
I've got a custom control that is trying to allow the user to enter a DateTime in a definable format and I was wondering if there was an easy way to alter the look of said control based on the...
7
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.