binary to decimal converter
binary to decimal converter
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.
Do you mean something like strtol()?
e.g. (in C):
Or a little more interesting, strtol.c:
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
e.g. (in C):
Code: Select all
#include <stdio.h>
int main() {
printf("%d\n",strtol("01111111",NULL,2));
}
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])));
}
or http://www.lernnetz-sh.de/kmLinux/doc/c ... S/strtol.c
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?
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?
Ah, QT. In that case I think you'll find your answer in this thread:
http://lists.trolltech.com/qt-interest/ ... 833-0.html
http://lists.trolltech.com/qt-interest/ ... 833-0.html