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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:04:33+00:00 2026-05-11T14:04:33+00:00

I have a project where a function receives four 8-bit characters and needs to

  • 0

I have a project where a function receives four 8-bit characters and needs to convert the resulting 32-bit IEEE-754 float to a regular Perl number. It seems like there should be a faster way than the working code below, but I have not been able to figure out a simpler pack function that works.

It does not work, but it seems like it is close:

$float = unpack('f', pack('C4', @array[0..3]);  # Fails for small numbers 

Works:

@bits0 = split('', unpack('B8', pack('C', shift))); @bits1 = split('', unpack('B8', pack('C', shift))); @bits2 = split('', unpack('B8', pack('C', shift))); @bits3 = split('', unpack('B8', pack('C', shift))); push @bits, @bits3, @bits2, @bits1, @bits0;  $mantbit = shift(@bits); $mantsign = $mantbit ? -1 : 1; $exp = ord(pack('B8', join('',@bits[0..7]))); splice(@bits, 0, 8);  # Convert fractional float to decimal for (my $i = 0; $i < 23; $i++) {     $f = $bits[$i] * 2 ** (-1 * ($i + 1));     $mant += $f; } $float = $mantsign * (1 + $mant) * (2 ** ($exp - 127)); 

Anyone have a better way?

  • 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. 2026-05-11T14:04:34+00:00Added an answer on May 11, 2026 at 2:04 pm

    I’d take the opposite approach: forget unpacking, stick to bit twiddling.

    First, assemble your 32 bit word. Depending on endianness, this might have to be the other way around:

    my $word = ($byte0 << 24) + ($byte1 << 16) + ($byte2 << 8) + $byte3; 

    Now extract the parts of the word: the sign bit, exponent and mantissa:

    my $sign = ($word & 0x80000000) ? -1 : 1; my $expo = (($word & 0x7F800000) >> 23) - 127; my $mant = ($word & 0x007FFFFF | 0x00800000); 

    Assemble your float:

    my $num = $sign * (2 ** $expo) * ( $mant / (1 << 23)); 

    There’s some examples on Wikipedia.

    • Tested this on 0xC2ED4000 => -118.625 and it works.
    • Tested this on 0x3E200000 => 0.15625 and found a bug! (fixed)
    • Don’t forget to handle infinities and NaNs when $expo == 255
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(.container).hover( function(){ $(.child-1).hide(0); $(.child-2).show(0); },function(){ $(.child-1).show(0); $(.child-2).hide(0); }); A project I have requires that
In a C project, I have a main() function in several files. When I
Possible Duplicate: Calling ActionScript 3 function from C# i have a project which is
I have a web project that I developed where one of the main functions
I have two WIN32 DLL projects in the solution, main.dll should call a function
Suppose I have a free function called InitFoo . I'd like to protect this
I have project that I'm working on that is going to require a webserver.
I have project Emle in Launchpad . I set it to import from emle.svn.sourceforge.net
I have a project that I'm currently working on but it currently only supports
I have a project that I would like to start beta testing soon, it

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.