Hi

I am on my way to learning perl, and am reading the beginning perl
book. In chapter 2 I am following the exercises
(http://docs.google.com/viewer?url=http%3A%2F%2Fblob.perl.org%2Fbooks%2Fbeginning-perl%2F3145_Chap02.pdf),
question 2 asks for a hex converter. As fun, I thought it would be
useful to do it the other way around convert numbers from decinal to
hex and oct and check the answers with assertions before printing.

I am using a guide found on perl monks to  convert from decimal to hex
http://www.perlmonks.org/?node_id=63074
There is a doc on perldoc for converting to oct from decimal
http://perldoc.perl.org/functions/oct.html
This there formula $oct_perm_str = sprintf "%o", $perms;
However it is not working for me. I have used my $oct = sprintf "%0", $number;

Can I have a little assistance why it fails to convert. ( I turned
assertion off when doing this)
c:\MyPerl>perl hexToOctal.plx
 What number would you like converted to a hexidecimal and octal? 15
0x0f
Invalid conversion in sprintf: "%0" at hexToOctal.plx line 11, <STDIN> line 1.
 15 is 1 as a hexidecimal and is %0 as an octal

This is the error.


#!/usr/bun/perl
#hextooct.plx
# decimal -> hex
# decimal -> oct
# dec == hex == oct
use warnings;
use strict;

print " What number would you like converted to a hexidecimal and octal? ";
my $number = <STDIN>;
chomp($number);
# number -> hexi -> octal
my $hex = printf("0x%02x\n", $number);
my $oct = sprintf "%0", $number;
# use assertions ' $hex == $oct == $number';
print " $number is ", $hex, " as a hexidecimal and is ", $oct, " as an
octal","\n";

Sayth

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to