For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2006 > decimal to hex converter









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author decimal to hex converter
dwg@dwgreer.net

2006-07-23, 9:56 pm

I'd like someone to check out a decimal to hexadecimal conversion
script that I wrote in perl. Check out the algorithm and let me know
how I can improve it. It's here: www.dwgreer.net/dechex.pl . Write to
dwg@dwgreer.net or reply to this post.

usenet@DavidFilmer.com

2006-07-23, 9:56 pm

dwg@dwgreer.net wrote:
> I'd like someone to check out a decimal to hexadecimal conversion
> script that I wrote in perl. Check out the algorithm and let me know
> how I can improve it.


You might review the perlfaq's first. For example, "perlfaq -q convert"
suggests a number of far better solutions (I prefer sprintf):

How do I convert from decimal to hexadecimal
Using sprintf:

$hex = sprintf("%X", 3735928559); # upper case A-F
$hex = sprintf("%x", 3735928559); # lower case a-f

Using unpack:

$hex = unpack("H*", pack("N", 3735928559));

Using Bit::Vector:

use Bit::Vector;
$vec = Bit::Vector->new_Dec(32, -559038737);
$hex = $vec->to_Hex();

And Bit::Vector supports odd bit counts:

use Bit::Vector;
$vec = Bit::Vector->new_Dec(33, 3735928559);
$vec->Resize(32); # suppress leading 0 if unwanted
$hex = $vec->to_Hex();

--
David Filmer (http://DavidFilmer.com)

dwg@dwgreer.net

2006-07-24, 6:57 pm


usenet@DavidFilmer.com wrote:
> suggests a number of far better solutions (I prefer sprintf):
>
> How do I convert from decimal to hexadecimal
> Using sprintf:
>
> $hex = sprintf("%X", 3735928559); # upper case A-F
> $hex = sprintf("%x", 3735928559); # lower case a-f



Thanks David,
I'm aware that there are subroutines to
convert between number systems available in perl. The idea here was to
code an algorithm myself and find out how I can improve it. It's just
a learning exercise.

usenet@DavidFilmer.com

2006-07-24, 6:57 pm

dwg@dwgreer.net wrote:
> The idea here was to code an algorithm myself and find out how
> I can improve it. It's just a learning exercise.


Understood.

Learning to use perlfaq is also a good learning exercise :^)

FWIW, I always am happy to recommend Dr. Damian Conway's book, "Perl
Best Practices" (O'Reilly) to folks who are learning Perl. If you're
gonna learn Perl, you might as well learn the "Best Practices" from the
very beginning. It saves a lot of time un-learning bad practices later
on (many Perl reference books and usenet postings aren't
"Conway-compliant," so don't assume that any code you see in a Perl
book reflects good programming style or technique).

I believe _PBP_ has many recommendations to offer for improvement of
your code (and nearly everyones code as well, including everything I've
ever written).

--
http://DavidFilmer.com

DJ Stunks

2006-07-24, 9:56 pm

dwg@dwgreer.net wrote:
> usenet@DavidFilmer.com wrote:
>
>
> Thanks David,
> I'm aware that there are subroutines to
> convert between number systems available in perl. The idea here was to
> code an algorithm myself and find out how I can improve it. It's just
> a learning exercise.


I've never really understood this argument. Why would you pick
something that's easily solved and solve it "your own way" in order to
learn? It is nonsensical to try to "improve" a script that has no
purpose. The improvement is to use the builtin that doesn't _convert_
a number at all, only shows you a different representation of the same
number.

Why don't you try to solve a real problem? this is the only way you're
going to learn how to write real Perl which performs a real purpose.

If you want an example of a problem to tackle, why don't you start with
this relatively straightforward question asked earlier this w on
this list:
http://groups.google.com/group/perl...6b?dmode=source
Post your solution and ask for comments.

Paul Lalli, a frequent contributor to this list, has, in the past,
posted a number of problems which one could try to solve. If you asked
him nicely I'm sure he'd post it again.

-jp

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com