in reply to Converting Hex to Float (and vise versa)
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Converting Hex to Float (and vise versa) (overly simple)
by Anonymous Monk on Feb 07, 2004 at 10:34 UTC | |
by BrowserUk (Patriarch) on Feb 07, 2004 at 15:55 UTC | |
by Anonymous Monk on Feb 07, 2004 at 21:27 UTC | |
by BrowserUk (Patriarch) on Feb 08, 2004 at 07:18 UTC |