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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:11:10+00:00 2026-05-26T00:11:10+00:00

I am wondering how to convert the result returned by float.hex() to binary, for

  • 0

I am wondering how to convert the result returned by float.hex() to binary, for example, from 0x1.a000000000000p+2 to 110.1.

Can anyone please help? Thanks.

  • 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-05-26T00:11:11+00:00Added an answer on May 26, 2026 at 12:11 am
    def float_to_binary(num):
        exponent=0
        shifted_num=num
        while shifted_num != int(shifted_num):        
            shifted_num*=2
            exponent+=1
        if exponent==0:
            return '{0:0b}'.format(int(shifted_num))
        binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)
        integer_part=binary[:-exponent]
        fractional_part=binary[-exponent:].rstrip('0')
        return '{0}.{1}'.format(integer_part,fractional_part)
    
    def floathex_to_binary(floathex):
        num = float.fromhex(floathex)
        return float_to_binary(num)
    
    
    print(floathex_to_binary('0x1.a000000000000p+2'))    
    # 110.1
    
    print(floathex_to_binary('0x1.b5c2000000000p+1'))    
    # 11.01101011100001
    

    Explanation:

    float.fromhex returns a float num. We’d like its binary representation.

    {0:b}.format(...) returns binary representations of integers, but not floats.

    But if we multiply the float by enough powers of 2, that is, shift the binary representation to the left enough places, we end up with an integer, shifted_num.

    Once we have that integer, we are home free, because now we can use {0:b}.format(...).

    We can re-insert the decimal point (err, binary point?) by using a bit of string slicing based on the number of places we had shifted to the left (exponent).

    Technical point: The number of digits in the binary representation of shifted_num may be smaller than exponent. In that case, we need to pad the binary representation with more 0’s on the left, so binary slicing with binary[:-exponent] won’t be empty. We manage that with '{0:0{1}b}'.format(...). The 0{1} in the format string sets the width of the formated string to {1}, padded on the left with zeros. (The {1} gets replaced by the number exponent.)

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

Sidebar

Related Questions

I was wondering how can I convert a coordinate that i acquired from a
I was wondering if anyone knows how to automatically convert PDF's to JPGs in
I'm wondering how to convert a hex string to a human readable string (if
I was wondering if I can somehow convert a column header text form MySQL
I am wondering whether anyone knows a easy way to convert a latex math
Hey, I'm just wondering how to convert a numerical date into text format in
I was wondering if it is possible to convert a wpf application to silverlight
I was wondering if there was an easy way in SQL to convert an
I was wondering what is the best way to convert excel sheet column names
I was wondering if there was a tool to convert a path data like

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.