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":
or perhaps you have a string of bytes whose hex values are 42, 8C, 00, and 00:my $string= "0x428C0000"; my $float= unpack "f", pack "L", hex $string;
in which case you could pick between these two, depending on whether the source and destination systems are the same or different endian:my $string= "\x42\x8C\x00\x00";
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):$float= unpack "f", $string; $float= unpack "f", scalar reverse $string;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |