How can I convert a string number to a number in Perl?

I need to convert decimals to binary and binary to decimals using a 24 bit number conversion system. Help?

  • I have the source code but I have no idea how to continue. If someone could show me the code and maybe explain why it works that would be awesome!! I'm completely new to this and am having difficulties teaching myself. Here's the source code. I'm using Visual Studios 2010 if that makes any difference: #include "stdafx.h" #include "Number.h" #include <iostream> #include <string> using namespace std; void process_DTB() ; void process_BTD() ; void process_EXIT(); void menu() ; bool valid(string,int) ; bool valid(int,int) ; const int SIZE = 24 ; int _tmain(int argc, _TCHAR* argv[]) { cout << "\n\tWelcome to 24-bit Two's Complement Number Conversion System\n\n"; while(true) { menu(); } return 0; } void menu(){ int choice ; cout << "\tOptions :\n\t\t1:decimal to binary\n\t\t2:binary to decimal\n\t\t0:Quit\n" ; cout << "\n\tYour choice:" ; cin >> choice ; switch(choice) { case 1 : process_DTB(); break ; case 2 : process_BTD() ; break ; case 0 : process_EXIT() ; break; default: cout << "\n\t*** Invalid choice****\n" << endl ; } } void process_DTB() { int n ; cout << "\n\tEnter an integer value you wish to convert: " ; cin >> n ; if ( !valid(n,SIZE)) { cout << "\n\t *** invalid value .... overflow " << endl << endl ; return ; } Number num(n) ; cout << "\n\t " << n << " = " << num.get_bin() << endl << endl; } void process_BTD() { cout << "\n\tEnter a binary number you wish to convert: " ; string bin ; cin >> bin ; // add your code here } void process_EXIT() { cout << "\n ... You are exiting 24-bit Two's Complement Number Conversion System...\n Bye!\n" << endl ; exit(0) ; } bool valid(int n, int size) { // make sure the number is within the valid range if ( n >= power(2,size) || n < -power(2,size)) return false ; else return true ; } bool valid(string s, int size) { // validate the string s is indeed binary and of length less or equal to size // put your code here }

  • Answer:

    Good homework question...

Batsmyma... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.