|
A simple Base10 to Base2
|
Email
|
| Submitted on: |
6/30/2001 5:31:23 AM |
| By: |
Josh
|
| Level: |
Beginner |
| User Rating: |
By 3 Users |
| Compatibility: |
5.0 (all versions) |
| Views: |
10380 |
|
(About the author) |
|
|
|
My first post here. Simply converts any Base 10 number to a binary number and at the same time does not show any leading zeros.
|
| |
Terms of Agreement:
By using this article, you agree to the following terms...
- You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
- You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
- You may link to this article from another website, but ONLY if it is not wrapped in a frame.
- You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
|
#!/usr/local/bin/perl
# Description: converts base 10 into base 2
# By: Josh
# Code: Free for anyoneto use/modify etc.. though it'd be cool that if
# you do use it as part of your code that you give me atleast some credit for it
print "Enter any number in base 10 (integer's only): ";
$base10 = <STDIN>;chomp($base10);
$copyBase10 = $base10;
while($base10){
$base10 = int($base10);
@baseList = reverse(split(/\^*/,$base10));
$intBase = int($baseList[0]/2);
$noIntBase = $baseList[0]/2;
if ($intBase eq $noIntBase){$holder = "0"}
else{$holder = "1"} push(@binarynum, $holder);
$base10 /= 2;
if($base10 < 1){last}
}
print "$copyBase10:\t" . reverse(@binarynum),"\n";
|
|
Report Bad Submission
|
Your Vote
|
| |
Other User Comments
|
7/15/2001 3:37:23 AM: Digital
Hi, just wanted to let you know theres a function called base_convert() that comes stock with PHP.. check it out, one line.. baseconvert(). (If this comment was disrespectful, please report it.)
|
7/21/2001 4:22:46 PM: josh
umm.. this isn't php this is perl. i don't know php (as of yet) (If this comment was disrespectful, please report it.)
|
7/30/2001 11:39:36 AM: David
Good observation Josh; PHP is better than Perl :D (If this comment was disrespectful, please report it.)
|
10/15/2001 8:28:49 PM: Josh
Yes i must agree w/ u. it is quite nicer (If this comment was disrespectful, please report it.)
|
1/1/2002 8:16:42 PM: T. E. Geek
I must disagree. Perl is, for it's purpose, superior in language design and functionality. If there is a function to do somthing in PHP, there is a module on cpan for perl to do the same-- and more. (If this comment was disrespectful, please report it.)
|
4/4/2003 1:38:04 AM:
wow... perl -vs- php flame! grrrr!! cat fight!
**GO PERL!** (If this comment was disrespectful, please report it.)
|
6/2/2004 1:40:03 AM:
(If this comment was disrespectful, please report it.)
|
6/2/2004 1:41:22 AM:
quote: "Theres more than one way to do it."
So true. I don't want to push my beliefs onto other people, but it is valid that you both have your languages you like, and that's what makes PSC special! (If this comment was disrespectful, please report it.)
|
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)
To post feedback, first please login.
|