C++十进制怎样转化为二进制
来源:百度知道 编辑:UC知道 时间:2024/05/21 15:44:19
C++中的函数 IntToBin
Returns a binary string representation for an Integer value.
function IntToBin(Value: cardinal): string;
Parameters
Value: cardinal
Cardinal value to be converted.
Returns
String - Binary representation of the Integer value.
Description
IntToBin is a String function used to construct a binary representation of a 32-bit Integer value. The return value for IntToBin will contain a string of "0" or "1" characters for each of the bits in the integer Value.
如果是求算法:
如下:
int k;//需要转换的十进只数.
int t=k;
bool muls=false;
if(k>=0)
musl=false; //判断是正负数.
char *Bin; //保存转换得到得2进制数组
int i=0;
for(;;)
{
if((t=t/2)!=0)
i++;
else
break;
}
Bin=new char[i+1];
t=k;
for(int j=i;j>0;j--)
{
Bin[j]=t%2;
t=t/2;
}
Bin[0]=(int