简单问题:求一函数,可以将十进制整数转化为二进制

superwt2001 2003-03-07 03:13:41
如题,注意输出二进制最好用字符串表示
...全文
197 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
prf8 2003-03-09
  • 打赏
  • 举报
回复
char * cnv(int x, char *a );
int main()
{
char b[32];//= new char[32];
cout<< cnv(45, b)<<endl;




return 0;

}

char *cnv(int x, char a[])
{
char *p = a;
while(x)
{
*a++ = x&0x1 ?'1':'0';
x>>=1;
}
*a = 0;
return p;
}
fireseed 2003-03-09
  • 打赏
  • 举报
回复
对不起,我犯了一个严重的错误


#include <math.h>
#include <stdio.h>

void fun( int nNumber, char *szBuffer, int nBufferSize )
{
const int nNumberSize = sizeof(int) * 8; // short改成int
for ( int i = 0; i < nNumberSize && i < nBufferSize - 1; i++ )
{
szBuffer[i] = ( ( ( nNumber & (int)pow( 2, nNumberSize - i - 1 ) ) == 0 ) ? '0' : '1' );
}
szBuffer[i] = 0;
}

void main()
{
unsigned int x = 46197;
char szTemp[256];
fun( x, szTemp, sizeof(szTemp) );
printf( szTemp );
}
fireseed 2003-03-09
  • 打赏
  • 举报
回复
有没有那么麻烦啊???

以下代码在VC.net下编译测试成功



#include <math.h>
#include <stdio.h>

void fun( int nNumber, char *szBuffer, int nBufferSize )
{
const int nNumberSize = sizeof(short) * 8;
for ( int i = 0; i < nNumberSize && i < nBufferSize - 1; i++ )
{
szBuffer[i] = ( ( ( nNumber & (int)pow( 2, nNumberSize - i - 1 ) ) == 0 ) ? '0' : '1' );
}
szBuffer[i] = 0;
}

void main()
{
unsigned short x = 46197;
char szTemp[256];
fun( x, szTemp, sizeof(szTemp) );
printf( szTemp );
}
csr1103 2003-03-09
  • 打赏
  • 举报
回复
楼上的用的是位运算。
看一下相关的书
楼上的楼上用的是书上讲的解题法。
来迟了。不说话了。
C++大学教程这本书里讲位运算刚好举的有这个例子。
allen1981813 2003-03-09
  • 打赏
  • 举报
回复
#include<iostream.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h> // ULONG_MAX
// Put out a portion of a string:
class fixw {
char* s;
public:
fixw(const char* S, int width);
~fixw();
friend ostream& operator<<(ostream&, fixw&);
};

fixw::fixw(const char* S, int width) {
s = (char*)malloc(width + 1);
assert(s);
strncpy(s, S, width);
s[width] = 0; // Null-terminate
}
fixw::~fixw() { free(s); }

ostream& operator<<(ostream& os, fixw& fw) {
return os << fw.s;
}
// Print a number in binary:
typedef unsigned long ulong;

class bin {
ulong n;
public:
bin(ulong N);
friend ostream& operator<<(ostream&, bin&);
};

bin::bin(ulong N) { n = N; }

ostream& operator<<(ostream& os, bin& b) {
ulong bit = ~(ULONG_MAX >> 1); // Top bit set
while(bit) {
os << (b.n & bit ? '1' : '0');
bit >>= 1;
}
return os;
}
void main() {
char* string ="Things that make us happy, make us wise";
for(int i = 1; i <= 10; i++)
cout << fixw(string, i) << endl;
ulong x = ULONG_MAX;
ulong y = 0x76543210UL;
cout << "x in binary: " << bin(x) << endl;
cout << "y in binary: " << bin(y) << endl;
}
ywchen2000 2003-03-09
  • 打赏
  • 举报
回复
void er(int n)
{
er(n/2);
cout<<n%2<<endl;
}
WeltactXp 2003-03-07
  • 打赏
  • 举报
回复
//刚好上次写过一个3码转换的类

//ForePart&BackPart:小数点前后的数字串被格式化成的数值型量
//比如输入18.75(字符串), 则ForePart is equal to 18, BackPart to 0.75
//这个方法是为换码做准备工作的
void KeSet::getInputAnd2Format(CString sInput)
{
int count = sInput.GetLength();
bool encounterPoint = false;

Initialize();

for(int i = 0; i < count; i ++)
{
if(sInput[i] == '+' || sInput[i] == '-') //过虑掉可能出现的正负符号
continue;

if(sInput[i] == '.')
{
encounterPoint = true;
continue;
}

if(encounterPoint == false)
ForePart += sInput[i];
else
BackPart += sInput[i];
}
}

//remain是小数点后保留的位数
CString KeSet::num2orgcode(const CString snum, int remain)
{
getInputAnd2Format(snum);

CString result = "";

int tempint = atoi(ForePart);
int div = tempint;

while(div != 0)
{
if(div%2 == 1)
result.Insert(0, '1');
else
result.Insert(0, '0');

div = div/2;
}

if(BackPart.GetLength() > 2)
{
result += ".";

double tempfloat = atof(BackPart);
for(int i = 0; i < remain; i ++)
{
tempfloat *= 2;
if(tempfloat >= 1)
{
result += "1";
tempfloat -= 1;
}
else
result += "0";
}
}

if(snum[0] == '-')
result.Insert(0, '1');
else
result.Insert(0, '0');

return result;
}
superwt2001 2003-03-07
  • 打赏
  • 举报
回复
各位大哥呀,hex,oct是可以的,但bin通不过
hzcsh 2003-03-07
  • 打赏
  • 举报
回复
dd(int i)
{
cout<<bin<<i<<endl;
}
就可以了
Tiangua 2003-03-07
  • 打赏
  • 举报
回复
bin 就是这里面的操作子 呀,

这样的操作子还有hex,oct~~~~
自己找一下吧, 也许我记错了………
呵呵……
FROM 2003-03-07
  • 打赏
  • 举报
回复
同学,我下午抽点时间给你写一个,抽不出时间就没办法了
superwt2001 2003-03-07
  • 打赏
  • 举报
回复
cwkxm(cwkxm) 你干啥哪!!广告?
superwt2001 2003-03-07
  • 打赏
  • 举报
回复
哪来的bin,编译通不过呀
cwkxm 2003-03-07
  • 打赏
  • 举报
回复
http://www.gupin.com/SoftView.Asp?SoftID=181
Tiangua 2003-03-07
  • 打赏
  • 举报
回复
main()
{int i;
cin>>i;
cout<<bin<<i<<endl;
}
其它的就自己看着办吧,
这是c++的语法……

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧