Conversion program decimal to hex and oct using assertions

Conversion program decimal to hex and oct using assertions

am 31.05.2011 06:27:53 von Sayth Renshaw

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.or g%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, 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 = ;
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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Conversion program decimal to hex and oct using assertions

am 31.05.2011 07:04:59 von Brandon McCaig

On Tue, May 31, 2011 at 12:27 AM, Sayth Renshaw wrote:
> Invalid conversion in sprintf: "%0" at hexToOctal.plx line 11, line 1.
*snip*
> my $oct = sprintf "%0", $number;

I think that you meant %O (or maybe %o; but not %0).

> my $hex = printf("0x%02x\n", $number);

I think that you meant sprintf here.


--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software

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

Re: Conversion program decimal to hex and oct using assertions

am 31.05.2011 07:24:34 von Michael Greb

On Tue, May 31, 2011 at 02:27:53PM +1000, Sayth Renshaw wrote:
> This there formula $oct_perm_str = sprintf "%o", $perms;
> However it is not working for me. I have used my $oct = sprintf "%0", $number;

You want '%o', the lower case letter o for the sprintf format string,
the sample code you showed used '%0' the number.

--
Michael
michael@thegrebs.com
M: +1-562-MIKEGRB

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

Re: Conversion program decimal to hex and oct using assertions

am 31.05.2011 07:39:53 von Sayth Renshaw

On Tue, May 31, 2011 at 3:24 PM, Michael Greb wrote:
> On Tue, May 31, 2011 at 02:27:53PM +1000, Sayth Renshaw wrote:
>> This there formula $oct_perm_str = sprintf "%o", $perms;
>> However it is not working for me. I have used my $oct = sprintf "%0", $number;
>
> You want '%o', the lower case letter o for the sprintf format string,
> the sample code you showed used '%0' the number.
>
> --
> Michael
> michael@thegrebs.com
> M: +1-562-MIKEGRB
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

Thanks I got it working with, that updated to be

my $hex = sprintf("ox%o2x\n", $number);
chomp($hex);
my $oct = sprintf "%o", $number;

When i turn on the assertion
use assertions ' $hex == $oct == $number ';

I am getting an error that an operator is required. Does that mean i
cannot compare a hex an octal and a decimal via equality in an
assertion?

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

Re: Conversion program decimal to hex and oct using assertions

am 31.05.2011 07:45:39 von Uri Guttman

>>>>> "SR" == Sayth Renshaw writes:

SR> my $hex = sprintf("ox%o2x\n", $number);

why is the \n there? you just chomp it off. better to not put it in the
string to begin with.

also that leading char is the letter 'o' which is not the way octal
numbers are represented. they start with just a zero. also what is the
2x part for?

SR> my $oct = sprintf "%o", $number;

SR> When i turn on the assertion
SR> use assertions ' $hex == $oct == $number ';

SR> I am getting an error that an operator is required. Does that mean i
SR> cannot compare a hex an octal and a decimal via equality in an
SR> assertion?

that statement has several problems. == returns a boolean value so you
can't chain them like that. secondly, the sprintf strings you are
getting are not converted to the numbers as you think they are. strings
are only converted to decimal values by default. you need the hex and/or
oct functions to explicitly convert a hex/octal string to a decimal
value which can then be compared.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

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

Re: Conversion program decimal to hex and oct using assertions

am 31.05.2011 10:05:53 von Sayth Renshaw

On Tue, May 31, 2011 at 3:45 PM, Uri Guttman wrote:
>>>>>> "SR" == Sayth Renshaw writes:
>
> =A0SR> my $hex =3D sprintf("ox%o2x\n", $number);
>
> why is the \n there? you just chomp it off. better to not put it in the
> string to begin with.
>
> also that leading char is the letter 'o' which is not the way octal
> numbers are represented. they start with just a zero. also what is the
> 2x part for?
>
> =A0SR> my $oct =3D sprintf "%o", $number;
>
> =A0SR> When i turn on the assertion
> =A0SR> use assertions ' $hex == $oct == $number ';
>
> =A0SR> I am getting an error that an operator is required. Does that mean=
i
> =A0SR> cannot compare a hex an octal and a decimal via equality in an
> =A0SR> assertion?
>
> that statement has several problems. == returns a boolean value so yo=
u
> can't chain them like that. secondly, the sprintf strings you are
> getting are not converted to the numbers as you think they are. strings
> are only converted to decimal values by default. you need the hex and/or
> oct functions to explicitly convert a hex/octal string to a decimal
> value which can then be compared.
>
> uri
>
> --
> Uri Guttman =A0------ =A0uri@stemsystems.com =A0-------- =A0http://www.sy=
sarch.com --
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------
>

The sprintf function was copied from Perl Monks
http://www.perlmonks.org/?node_id=3D63074, I am only finishing chapter 2
in beggnng Perl so I don't fully understand it yet.

> are only converted to decimal values by default. you need the hex and/or
> oct functions to explicitly convert a hex/octal string to a decimal
> value which can then be compared.

Does this mean I could not directly compare a hex created from a
decimal with a decimal?

so hex == dec && oct == dec && hex == oct.

sayth

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

Re: Conversion program decimal to hex and oct using assertions

am 31.05.2011 17:41:40 von Uri Guttman

>>>>> "SR" == Sayth Renshaw writes:

>> Uri Guttman =A0------ =A0uri@stemsystems.com =A0-------- =A0http://www=
..sysarch.com --
>> ----- =A0Perl Code Review , Architecture, Development, Training, Suppo=
rt ------
>> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.=
com ---------
>>=20

please don't quote signatures. edit the quoted email to show only the
parts you are replying to.

SR> The sprintf function was copied from Perl Monks
SR> http://www.perlmonks.org/?node_id=3D63074, I am only finishing chapte=
r 2
SR> in beggnng Perl so I don't fully understand it yet.

so wait until you understand it to use it! :)

>> are only converted to decimal values by default. you need the hex and/=
or
>> oct functions to explicitly convert a hex/octal string to a decimal
>> value which can then be compared.

SR> Does this mean I could not directly compare a hex created from a
SR> decimal with a decimal?

no. perl only stores numbers in one way, as integers (we aren't getting
into floats or binary format here). only when you convert to/from a
string does decimal/hex/oct come into play.

SR> so hex == dec && oct == dec && hex == oct.

well, if that were legal perl it would be better. and you have one extra
unneeded comparison. once the first two pass, the third must always pass.

uri

--=20
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com =
--
----- Perl Code Review , Architecture, Development, Training, Support ----=
--
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com -------=
--

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