LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2005, 08:54 PM   #1
audibel
Member
 
Registered: Oct 2004
Distribution: Debian Sarge
Posts: 177

Rep: Reputation: 30
cout conversion to binary...


I just wrote a program, the first semi-useful C++ program I've ever written, that converts to and from dec, hex, and oct... and I was wondering if cout had a conversion for binary as well? It took me a long time to write and I think it's pretty nifty, but would like binary conversion as well, I can enter the flags and number at the command line (which was the really hard part, because it wouldn't have taken all of 10 minutes to do with cin) I'm sure there are plenty of them out there but this one is mine :P

And if you've got some spare time maybe you could critique my code, and tell me what I could improve on, or what I did well. I know the focus of C++ is OOP so maybe you could tell me how I could work OOP into this, it's not a big program, so the functions aren't overwhelming or anything, but I'm still trying to grasp the usefulness of OOP.

oh and could anybody point out the difference between chr* and string for me, I'm still a little befuddled by that, because strtoul won't take a string, and it caused me a lot of trouble. Is there a way to convert from string to integer with the string class?

And one more thing, anybody have suggestions as to how to get rid of those nasty nested ifs... I hate those things, In some other languages they have a switch case where you can compare things other than integers... any other alternatives in C++

Code:
 //ctohdo.cpp - converts a decimal, hexadecimal, or octal number
//written by Gary Boyd

#include <iostream>
#include <cstdlib>


using namespace std;
const char* shex = "-h";  //hex flag
const char* sdec = "-d";  //dec flag
const char* soct = "-o";  //oct flag
int strtoint(char* arg, int base);  // converts string to integer
int ctohex(int val);  //conversion to hex
int ctooct(int val);  //conversion to oct
int ctodec(int val);  //conversion to dec
int typeconvert(string arg);  //the type from which to convert
int flagpass(string arg1, char* arg2, string arg3);  //passes information for conversion

int strtoint(char* arg, int base)
{
  int i = strtoul(arg, NULL, base);
  return i;
}
int flagpass(string arg1, char* arg2, string arg3)
{
  if(arg1 == shex)
    {int i = strtoint(arg2, typeconvert(arg3));
     ctohex(i);}
     
  else
  {
    if(arg1 == sdec)
      {int i = strtoint(arg2, typeconvert(arg3));
       ctodec(i);}
    else
    {
       if(arg1 == soct)
         {int i = strtoint(arg2, typeconvert(arg3));
          ctooct(i);}
       else
       return 1;
    }
 }      
}
int typeconvert(string arg)  //takes first arg from command line
{          //and uses it as the base for strtoul 
  if(arg == shex)
    {return 16;}
  else
  {
    if(arg == sdec)
      {return 10;}
    else
    {
       if(arg == soct)  
         {return 8;}
       else
       return 10;
    }
  }
}      
int ctohex(int val)
{ cout << hex << val << endl; return 0;}
int ctodec(int val)
{ cout << dec << val << endl; return 0;}
int ctooct(int val)
{ cout << oct << val << endl; return 0;}

int main(int nargs, char* listargs[])
{
  string flag2 = listargs[1]; // from this arg type
  string flag = listargs[2];  // to this arg type
  char* num = listargs[3];

  flagpass(flag, num, flag2);
  return 0;
}

Last edited by audibel; 03-10-2005 at 09:00 PM.
 
Old 03-11-2005, 07:20 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
My knowledge about C++ is fading, but I once made a C-function that makes a string (C-style char pointer string) with the binary representation of an integer value. Lke most of the times, this C-function is usable in C++.

Code:
#include <iostream>

// Number of bits the binaray string will contain
#define NUMBITS 8   

using namespace std;


char *binarystring(char *str, int x)
{
    int i;

    for (i = 0; i < NUMBITS; ++i)
        str[NUMBITS - i - 1] = x & 1 << i ? '1' : '0';
    str[NUMBITS] = '\0';
    return str;
}


int main(void)
{
    int val;
    char binstring[NUMBITS + 1];

    // Some value;
    val = 50;

    // Print hex
    cout << "Hex = " << hex << val << endl;

    // Print binary
    cout << "Bin = "  << binarystring(binstring, val) << endl;
    
    return 0;
}
 
Old 03-11-2005, 01:36 PM   #3
audibel
Member
 
Registered: Oct 2004
Distribution: Debian Sarge
Posts: 177

Original Poster
Rep: Reputation: 30
Thanks for the code to look at, but I was wanting to convert from the command line argument char* listofargs, to binary... which means I need to convert from string to integer (at least I think that's what it means)...
 
Old 03-12-2005, 07:06 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by audibel
but I was wanting to convert from the command line argument char* listofargs, to binary...
In your program, you already do that. So just extend your program to recognize "bin" as a argument ("flag"), and then call binarystring() to do the conversion.

Quote:
which means I need to convert from string to integer (at least I think that's what it means)... [/B]
You already made strtoint() to do that.
This is no different for converting to binary.
As a side note, it may be easier to use atoi().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
undefined symbol cout Tazzie76 Linux - Software 8 12-10-2004 02:09 PM
c++ cout not working :( Longinus Programming 10 08-01-2004 07:32 PM
C++ boolean in cout statement AMMullan Programming 2 06-08-2004 12:44 PM
cout queues miguetoo Programming 4 03-17-2004 02:02 AM
c++ binary to decimal conversion.. miguetoo Programming 13 04-24-2003 09:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration