binary to decimal converter

Discuss Programming
Post Reply
shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

binary to decimal converter

Post by shuiend »

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.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

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

shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Post by shuiend »

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?

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main »

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

http://lists.trolltech.com/qt-interest/ ... 833-0.html

shuiend
scripter
scripter
Posts: 91
Joined: Mon Apr 28, 2003 8:05 pm

Post by shuiend »

Thanks Void Main you are a genoius. I am still learning qt and that maling list link really halped out.

Post Reply