A slightly deranged piece of PDL code I wrote to rapidly decode lots of floats at scale from binary STL files, at https://github.com/PDLPorters/pdl/blob/5b6057ffed3ffc1fe2f8018b28299ecf30e36f35/IO/STL/STL.pm#L152-L170:
sub _read_binary { my ($fh) = @_; barf "bigfloat" unless(length(pack("f", 1)) == 4); # TODO try to read part name from header (up to \0) seek($fh, 80, 0); my $buf; read($fh, $buf, 4) or warn "EOF?"; my $triangles = unpack(' +L<', $buf); my $bytes = 50 * $triangles; # norm+3vertices * 3float + short with +length of extra my $bytespdl = zeroes PDL::byte(), 50, $triangles; my $bytesread = read($fh, ${$bytespdl->get_dataref}, $bytes); barf "Tried to read $bytes but only got $bytesread" if $bytesread != + $bytes; $bytespdl->upd_data; my $floatpdl = zeroes PDL::float(), 3, 4, $triangles; ${$floatpdl->get_dataref} = ${$bytespdl->slice('0:47')->get_dataref} +; $floatpdl->upd_data; $floatpdl->type->bswap->($floatpdl) if isbigendian(); # TODO check that the unit normal is within a thousandth of a radian # (0.001 rad is ~0.06deg) _as_ndarray($floatpdl->slice(':,1:3')); }

In reply to Re: Converting bytes to floats by etj
in thread Converting bytes to floats by Anonymous Monk

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.