基于库函数实现十进制转二进制,二进制转十进制。
{二进制以字符串形式表示,不含除"0", "1"外其他字符}
Language support(Up to now 16.9.14): C/C++ , Python
C/C++:
Dec->Bin
#include <stdio.h>
#include <stdlib.h>
char buffer[9] = {0};
_itoa( _integer, buffer, 2 ); // 8 bit
E.g.
int _integer = 8;
_itoa( _integer, buffer, 2);
cout << buffer;
1000
Bin->Dec
#include <stdio.h>
#include <stdlib.h>
string str_bin = "00011";
char *stop;
int _integer = strtol(str_bin.substr(0,4).c_str(), &stop, 2); // long -> int
cout << _integer;
1
C++新增std: stoi
Python:
Dec->Bin
bin(_integer)[2:]
E.g.
_integer = 10
bin(_integer)
'0b1010'
bin(_integer)[2:]
'1010'
Bin->Dec
int(str_bin, 2)
E.g.
str_bin = "0001"
int(str_bin, 2)
1