BEGIN { my $code= ""; if( pack("L",1) ne pack("N",1) ) { $code= "''.reverse "; } eval "sub to_big_endian { ${code}pop }; 1" or die $@; } sub to_bin { my( $num )= @_; my( $sign, $exp, $mant )= unpack "A1A11A*", unpack "B*", to_big_endian pack"d",2*$num+1; $mant =~ s/10*$//; return "1".$mant; }