#######################################################################
# This software has been created by Genome Research Limited (GRL).    # 
# GRL hereby grants permission to use, copy, modify and distribute    # 
# this software and its documentation for non-commercial purposes     # 
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  # 
# otherwise in connection with the software, its use or the use of    # 
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible # 
# or liable for any loss or damage whatsoever arising in any way      # 
# directly or indirectly out of the use of this software or its       # 
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out # 
# above, and must contain this copyright notice.                      #
#######################################################################
#
# sexagesimal.pm: A module to convert numeric values (dec, octal, hex) into
# a base60 value stored in a string, and vice versa.
#
# $Id: Sexagesimal.pm,v 1.2 2003/09/23 08:06:12 rmd Exp $
#
package Sexagesimal;

use Exporter;
use strict;
use vars qw(@ISA @EXPORT);

# our (@ISA, @EXPORT);
@ISA       = qw(Exporter);
@EXPORT    = qw(int2sexa sexa2int);

=pod

=head1 NAME

    Sexagesimal - Conversion between integer to base60 (strings).

=head1 SYNOPSIS

    use Sexagesimal;
    $val = 0xdeadbeef;
    $sexa = int2sexa($val);
    print "$val in base60 = $sexa\n";
    $val = sexa2int($sexa);
    print "$sexa as an integer = $val\n";

=head1 METHODS

=cut

# -----------------------------------------------------------------------------
my @sexacodes;
my %intcodes;
for (my $i = 0; $i < 10; $i++) {
    $sexacodes[$i] = chr(ord("0")+$i);
    $intcodes{chr(ord("0")+$i)} = $i;
}
for (my $i = 0; $i < 26; $i++) {
    $sexacodes[$i+10] = chr(ord("A")+$i);
    $intcodes{chr(ord("A")+$i)} = $i+10;}


for (my $i = 0; $i < 26; $i++) {
    $sexacodes[$i+36] = chr(ord("a")+$i);
    $intcodes{chr(ord("a")+$i)} = $i+36;
}


# -----------------------------------------------------------------------------
=pod

=head2 int2sexa:

    Converts an integer value (the only argument) to a string holding
    the sexagesimal notation. Returns this string.

    $sexa = int2sexa(100);

=cut
sub int2sexa {
    my ($int) = @_;
    my $sexa = "";
    do {
	$sexa = $sexacodes[$int % 60] . $sexa;
	$int = int($int/60);
    } while ($int);
    return $sexa;
}

# -----------------------------------------------------------------------------
=pod

=head2 sexa2int:

    Converts a sexagesimal value encoded in a string to an integer
    value. The integer value is returned.

    $val = sexa2int("1e");

=cut
sub sexa2int {
    my ($sexa) = @_;
    my $int = 0;
    foreach my $c (split("", $sexa)) {
	$int = $int * 60 + $intcodes{$c};
    }
    return $int;
}

1;

# -----------------------------------------------------------------------------
__END__

=head1 AUTHOR

James Bonfield (jkb@sanger.ac.uk)

=head1 HISTORY

 # $Log: Sexagesimal.pm,v $
 # Revision 1.2  2003/09/23  08:06:12  rmd
 # Changed 'our' declaration to 'use vars' as our doesn't work with old versions
 # of perl.
 #
 # Revision 1.1  2003/09/22  16:11:39  jkb
 # Initial revision
 #

=cut
