This should work on many platforms for the example data you gave:

my $float= unpack "f", pack "L", 0x428C0000;

(assuming the value you wanted was the nice-looking "70").

but I'm not sure how to reconcile the terms "in ASCII" vs. "e.g. 0x428C0000". Perhaps you have a string like "0x428C0000":

my $string= "0x428C0000"; my $float= unpack "f", pack "L", hex $string;
or perhaps you have a string of bytes whose hex values are 42, 8C, 00, and 00:
my $string= "\x42\x8C\x00\x00";
in which case you could pick between these two, depending on whether the source and destination systems are the same or different endian:
$float= unpack "f", $string; $float= unpack "f", scalar reverse $string;
or you could use use two steps to auto-handle the receiving ends endianness (since your example data appears to show me the endianness of the source):
my $string= "\x42\x8C\x00\x00"; my $float= unpack "f", pack "L", unpack "N", $string;

- tye        


In reply to Re: Converting Hex to Float (and vise versa) (overly simple) by tye
in thread Converting Hex to Float (and vise versa) by unstable_geek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.