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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:51:37+00:00 2026-06-03T02:51:37+00:00

I am trying to convert hex value to float using (Python 2.7) the following

  • 0

I am trying to convert hex value to float using (Python 2.7) the following method:

def hex2float(x):
    y = 0
    z = x.decode('hex') 
    try:
        y = struct.unpack('!f', z)[0]
    except:
        print sys.exc_info()[1]    
    print 'z = ' + z 
    print 'y = %s' % (y) 
    print 'x = ' + x
    return

def foo28():
    x = '615885'   #8.9398e-039
    hex2float(x)

The output is as follows:

unpack requires a string argument of length 4
z = aXà
y = 0
x = 615885

I notice that I get the exception message for really small values. Is there a proper way to convert hex values to floating values for such cases.

  • 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-03T02:51:38+00:00Added an answer on June 3, 2026 at 2:51 am

    You need four bytes to unpack, so prepend null bytes if necessary:

    z = x.decode('hex') 
    z = '\0' * (4 - len(z)) + z
    

    Normally str.decode only outputs as much bytes as necessary to represent the value, so that’s why you only see it happen for small values.

    This works perfectly:

    >>> z = '615885'.decode("hex")
    >>> z = '\0' * (4 - len(z)) + z
    >>> struct.unpack('!f', z)
    (8.939797951825212e-39,)
    

    If you’re going to do doubles as well this solution still works, just change 4 to 8.

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

Sidebar

Related Questions

I'm trying to translate the following Python code into C++: import struct import binascii
I am trying to convert the input ascii string to hex format using the
I am trying to convert FFAD (hex) to a decimal value and then do
I'm trying to convert a Hex NSString to NSData (I'm using the below attached
I am trying to convert a HEX-sequence to a String encoded in either, ISO-8859-1,
I'm trying to convert a string of hex digits to a binary string. If
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
Trying to convert the following but am having difficulty. Can anyone see where I'm
I am trying to convert a string value into a name of an attribute
I'm trying to read the bitmap header. I have defined the following struct: typedef

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.