binary to decimal converter

Discuss Programming

binary to decimal converter

Postby shuiend » Mon Dec 08, 2003 9:18 pm

I have a string of ones and zeros. it is a binary number and i want to change it to a decimal int. How would i do this in c++? I just need a quick function that would do it. I am not picky on how it works just as long as it works.
shuiend
scripter
scripter
 
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Postby Void Main » Mon Dec 08, 2003 9:40 pm

Do you mean something like strtol()?

e.g. (in C):
Code: Select all
#include <stdio.h>
int main() {
  printf("%d\n",strtol("01111111",NULL,2));
}


Or a little more interesting, strtol.c:
Code: Select all
#include <stdio.h>
int main(int argc,char **argv) {
  if (argc != 3) {
    printf(" Syntax: strtol numstr base\n");
    printf("Example: strtol 010101 2\n");
    exit(1);
  }
  printf("%s in base %s = %i in base 10\n",argv[1],argv[2],strtol(argv[1],NULL,atoi(argv[2])));
}


I know, it's not C++ but I'm too old to learn new tricks.

or http://www.lernnetz-sh.de/kmLinux/doc/c ... S/strtol.c
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby shuiend » Tue Dec 09, 2003 5:45 am

ok i added a function to my program tha does the strtol("01111111",NULL,2); line. But i am getting errors.

guiqt.cpp:24: cannot convert `QTextStream&(*)(QTextStream&)' to `const char*'
for argument `1' to `long int strtol(const char*, char**, int)'

I am using QT for my GUI and QT keeps thinking that i am using its QTextStream which I am not. So should i just make another .cpp file and add the function in there that does it?
shuiend
scripter
scripter
 
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Postby Void Main » Tue Dec 09, 2003 9:22 am

Ah, QT. In that case I think you'll find your answer in this thread:

http://lists.trolltech.com/qt-interest/ ... 833-0.html
User avatar
Void Main
Site Admin
Site Admin
 
Posts: 5705
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA

Postby shuiend » Tue Dec 09, 2003 7:43 pm

Thanks Void Main you are a genoius. I am still learning qt and that maling list link really halped out.
shuiend
scripter
scripter
 
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests