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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 216k
  • Answers 216k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Visual Basic Version: Dim tb As New TextBlock Dim b… May 12, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can do anythings during MenuItemClick event handler. May 12, 2026 at 11:11 pm
  • Editorial Team
    Editorial Team added an answer Yes Dojo works in the same fashion. There should be… May 12, 2026 at 11:11 pm

Related Questions

So I've done unit testing some, and have experience writing tests, but I have
I am cleaning up the DataReaders in an old .NET 1.1 project that I
This is a long one, The Premise Was called in to help out a
A recent project called for importing data into an Oracle database. The program that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.