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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:01:42+00:00 2026-06-17T18:01:42+00:00

Python 2.6 on Redhat 6.3 I have a device that saves 32 bit floating

  • 0

Python 2.6 on Redhat 6.3

I have a device that saves 32 bit floating point value across 2 memory registers, split into most significant word and least significant word.
I need to convert this to a float.
I have been using the following code found on SO and it is similar to code I have seen elsewhere

#!/usr/bin/env python
import sys
from ctypes import *
first  = sys.argv[1]
second = sys.argv[2]

reading_1 = str(hex(int(first)).lstrip("0x"))
reading_2 = str(hex(int(second)).lstrip("0x"))

sample = reading_1 + reading_2

def convert(s):
    i = int(s, 16)                   # convert from hex to a Python int
    cp = pointer(c_int(i))           # make this into a c integer
    fp = cast(cp, POINTER(c_float))  # cast the int pointer to a float pointer
    return fp.contents.value         # dereference the pointer, get the float

print convert(sample)

an example of the register values would be ;

register-1;16282 register-2;60597

this produces the resulting float of

1.21034872532

A perfectly cromulent number, however sometimes the memory values are something like;

register-1;16282 register-2;1147

which, using this function results in a float of;

1.46726675314e-36

which is a fantastically small number and not a number that seems to be correct. This device should be producing readings around the 1.2, 1.3 range.

What I am trying to work out is if the device is throwing bogus values or whether the values I am getting are correct but the function I am using is not properly able to convert them.

Also is there a better way to do this, like with numpy or something of that nature?
I will hold my hand up and say that I have just copied this code from examples on line and I have very little understanding of how it works, however it seemed to work in the test cases that I had available to me at the time.

Thank you.

  • 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-17T18:01:44+00:00Added an answer on June 17, 2026 at 6:01 pm

    If you have the raw bytes (e.g. read from memory, from file, over the network, …) you can use struct for this:

    >>> import struct
    >>> struct.unpack('>f', '\x3f\x9a\xec\xb5')[0]
    1.2103487253189087
    

    Here, \x3f\x9a\xec\xb5 are your input registers, 16282 (hex 0x3f9a) and 60597 (hex 0xecb5) expressed as bytes in a string. The > is the byte order mark.

    So depending how you get the register values, you may be able to use this method (e.g. by converting your input integers to byte strings). You can use struct for this, too; this is your second example:

    >>> raw = struct.pack('>HH', 16282, 1147)    # from two unsigned shorts
    >>> struct.unpack('>f', raw)[0]              # to one float
    1.2032617330551147
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python beginner here. I have a text file that is sorted into columns: fields
On Redhat, python 2.4.3 is installed. On that machine, we have an application that
[Python/MySQLdb] - CentOS - Linux - VPS I have a page that parses a
I have 24,888 records that go into a SQLite table with FTS indexing. I
I've run into issue. I have a python script. You can call it CGI
I have a python script that I want to debug with python-mode . I
I have a C++ application from Windows that I wish to port across to
Python 3 really complicated the whole file reading process, when you have a binary
Python beginner here, I have a list of lists and want to refer to
Python is so dynamic that it's not always clear what's going on in 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.