Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:32:05+00:00 2026-06-03T09:32:05+00:00

I’d like to convert an array of integer values I have received over USB

  • 0

I’d like to convert an array of integer values I have received over USB using pyusb to a list of hex values. I’d like these hex values to always have two bytes, i.e. 0x##. However the normal hex() function will return 0x1 with an input of 1. I’d like it to return 0x01.

Then after having the list of hex values I’d like to append them together while throwing away the ‘0x’ portion. This is what I currently have, pretty simple.

data_read = dev.read(0x82,64,0,1000)
hex_data = range(0,len(data_read))

for i in range(0,len(data_read)):
    hex_data[i] = hex(data_read[i])

Any ideas? I can fudge it and do it the sloppy way, but I was hoping there is a proper way to do it. Thank you.

Update:

 data_read = dev.read(0x82,64)
 print data_read
 >>> array('B', [169, 86, 128, 1, 0, 128])
 for i in range(0,len(data_read)):
      hex_data[i] = hex(data_read[i])
 print hex_data
 >>> ['0xa9', '0x56', '0x80', '0x1', '0x0', '0x80']

The method suggested by Lavon did not work since the resulting hex values were technically strings in my code? Instead I just skipped my whole for loop converting to hex and directly did as Lavon and moooeeeep suggested and it worked! Thanks!

hex_data = ('%02x' %i for i in data_read)
print ''.join(hex_data)
>>> a95680010080

Is there a good reference you use for syntax? i.e. the ‘%02x’ you used? I haven’t seen that before and would like to understand it better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:32:07+00:00Added an answer on June 3, 2026 at 9:32 am

    Does this fit the bill?

    data = [0x1, 0x4, 0x5, 0xFF, 0x12]
    
    new_data = ['%02X' %i for i in data]
    print ''.join(new_data)
    

    yields:

    010405FF12
    

    using list comprehension and the join operation.

    Alternatively, as suggested by @moooeeeep here is the solution using a generator:

    new_data = ('%02X' %i for i in data)
    print ''.join(new_data)
    

    For the string formatting options, see this

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.