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 7831169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:36:20+00:00 2026-06-02T11:36:20+00:00

I feel like a complete tool for posting this, it is so basic and

  • 0

I feel like a complete tool for posting this, it is so basic and I cant believe I have wasted the last two days on this problem. I’ve tried all the solutions I can find on this (seriously, I will show you my internet history) but to no avail. Here is the problem:

I am parsing a serial string in from a uC. It is 52 bytes long and contains a lot of different variables of data. The data in encoded in packed binary coded decimal.

Ex: .....blah.....0x01 0x5E .....blah

015E hex gives 350 decimal. This is the value I want. I am reading in the serial string just fine, I used binascii.hexifiy to print the bytes to ensure it is corrent. I use

data = ser.read()

and placed the data in an array if an newline is not received. I have tried making the array a bytearray, list, anything that I could find, but none work.

I want to send the required two byte section to a defined method.

def makeValue(highbyte, lowbyte)

When I try to use unpack, join, pack, bit manipulation, string concentation, I keep getting the same problem.

Because 0x01 and 0x5E are not valid int numbers (start of heading and ^ in ASCII), it wont work. It wont even let me join the numbers first because it’s not a valid int.

using hex(): hex argument can't be converted to hex.
Joining the strings: invalid literal for int() with base 16: '\x01^'
using int: invalid literal for int() with base 10: '\x01^'
Packing a struct: struct.error: cannot convert argument to integer

Seriously, am I missing something really basic here? All the examples I can find make use of all the functions above perfectly but they specificy the hex numbers ‘0x1234’, or the numbers they are converting are actual ASCII numbers. Please help.

EDIT
I got it, ch3ka set me on the right track, thanks a million!

I don’t know why it wouldn’t work before but I hex’ed both values

one = binascii.hexlify(line[7])
two = binascii.hexlify(line[8])
makeValue(one, two)` 

and then used the char makeValues ch3ka defined:

def makeValue(highbyte, lowbyte)
    print int(highbyte, 16)*256 + int(lowbyte, 16)

Thanks again!!!

  • 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-02T11:36:26+00:00Added an answer on June 2, 2026 at 11:36 am

    you are interpreting the values as chars. Feeding chars to int() won’t work, you have to feed the values as strings, like so: int("0x5E", 16). What you are attempting is in fact int(chr(int("0x5E", 16)),16), which is int("^",16) and will of course not work.

    Do you expect these results?

    makevalue('0x01', '0x5E') ->  350 0x15e 0b101011110
    makevalue('0xFF', '0x00') ->  65280 0xff00 0b1111111100000000
    makevalue('0x01', '0xFF') ->  511 0x1ff 0b111111111
    makevalue('0xFF', '0xFF') ->  65535 0xffff 0b1111111111111111
    

    If so, you can use this:

    def makeValue(highbyte, lowbyte):
        return int(highbyte, 16)*256 + int(lowbyte, 16)
    

    or the IMO more ugly and errorprone:

    def makeValue(highbyte, lowbyte):
        return int(highbyte+lowbyte[2:], 16) # strips leading "0x" from lowbyte be4 concat
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so I feel like a complete idiot asking this, but I've been looking
I feel like I've solved this problem 10 times before but I can never
Feel like this should be something easy that I'm missing. I have a table
I feel like this must have been asked, but I'm unable to find it
I feel like this question must have been asked before but I must not
I feel like this is something I should already know, but I'm just not
I feel like this is easy but I am missing something... Using jQuery, I
I feel like this should be a no brainer, but clearly I'm missing something...
I feel like GUID/UUID questions have been done to death here, but I can't
I feel like this is a stupid question, but I can't think of a

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.