Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

RE: floating point convertion
From: Mark Cohen <mailto:m.cohen@tiscalinet.it> wrote:

: Hello ,
:
: I have a transferred a file from an IBM mainframe
: to a windows platform that I need to analyse. The
: file contains an 8 byte floating point hexadecimal
: representaion 44FE880000000000.
:
: This should be converted to the number 65160.

When I use this sub I get 1.21711040165713e-008
not 65160.

print floatmvs( '44FE880000000000' );


: sub floatmvs {
:  my $mat=0;
:  my $firstbyte = unpack "H2", $_[0];
:  my $exp=$firstbyte-40;  # base 16
:  my $bin=unpack('B*',substr($_[0],1,7));
:    for ($start=0; $start <56; $start+=1) {
:         $bit=substr($bin,$start,1);
:         $bitpos=$start+1;
:         if ($bit == 1) {
:             $val=(1/2)**($bitpos);
:             $mat=$mat+$val;
:         }
:    }
:    my $num=$mat*(16**$exp);
:    return $num;
: }

With 'strict' and 'warnings' turned on, I get
the same result with this.

use strict;
use warnings;

print floatmvs2( '44FE880000000000' );

sub floatmvs2 {
my @bits = split //, unpack 'B*', substr( $_[0], 1, 7 );

my $mat  = 0;
foreach my $pos ( 0 .. $#bits ) {
$mat += $bits[ $pos ] * ( 1 / 2 ) ** ( $pos + 1 );
}

my $exp = unpack( 'H2', $_[0] ) - 40;
return $mat * ( 16 ** $exp );
}


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328




Report this thread to moderator Post Follow-up to this message
Old Post
Charles K. Clarkson
08-05-04 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:35 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.