#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include MODULE = Math::Tobase PACKAGE = Math::Tobase SV * tobase(radix, num) int radix long num CODE: ST(0) = sv_newmortal(); if (2 <= radix && radix <= 62 || -36 <= radix && radix <= -2) { mpz_t big; mpz_init_set_si(big, num); if (mpz_sizeinbase(big, radix) <= 70) { char buf[72]; mpz_get_str(buf, radix, big); sv_setpv(ST(0), buf); } mpz_clear(big); } #### package Math::Tobase; require Exporter; require DynaLoader; our $VERSION = "1.00"; our @ISA = (Exporter::, DynaLoader::); our @EXPORT = "tobase"; bootstrap Math::Tobase::; 1; __END__ #### =head1 NAME Math::Tobase - Convert integers to a string in any radix =head1 SYNOPSIS use Math::Tobase; print tobase(16, 200), "\n"; # prints c8 =head1 DESCRIPTION =over 4 =item tobase RADIX, INTEGER Converts an integer to a string in any radix (number base), returning a string. The radix must be an integer between 2 and 62 or between -36 and -2, inclusive. If the magnitude of the radix is greater than 10 but at most 36, lowercase letters a to z are used to indicate digits greater than 9; however, if the radix is greater than 36 then digits between 10 and 35 are represented with uppercase, and digits greater than 35 are represented with lowercase letters. =back =head1 INSTALLATION This module requires the gmp (GNU MP) library, which you can download from L. =cut #### use ExtUtils::MakeMaker; WriteMakefile( NAME => "Math::Tobase", VERSION_FROM => "lib/Math/Tobase.pm", LIBS => ["-lgmp"], ); #### perl Makefile.PL && make #### perl -I./lib -Iblib/arch -we 'use Math::Tobase; print tobase(13, 54), "\n";'