我希望内存中信息原样输出.怎么实现呢?
eg:
int i = 65;
在内存中存放形式为:000001 000001
我希望我从内存中直接得到000001 000001.因为我需要对这些二进制串进行重新编码.
怎么实现呢?
#include <stdio.h>
main()
{
unsigned long i;
int j,k;
unsigned char *Ptr,temp,temp1;
for (i=0;i<65536;i++)
{
Ptr=(char *)i;
temp=*Ptr;
k=128;
for (j=1;j<9;j++)
{temp1=temp/k;
temp=temp-temp1*k;
k=k/2;
printf ("%d",temp1);
}
printf ("%5d",*Ptr);
printf ("\n");
getch();
}
getch();
}
需要指定地址时,只需将FOR语句换为Scanf语句。
[此贴子已经被作者于2007-5-19 17:15:43编辑过]
过去写的
这个程序能显示任何类型的内存单元的01码
#include <iostream>
using namespace std;
template <typename OStream,typename T1>
void GetBinary(OStream outs,T1& in)
{
T1 data = in;
unsigned char c = 1<<7,j = 0,k;
const unsigned char* p = (unsigned char*)&data+sizeof(data)-1;//转到高地址
for(int i = 0 ;i<sizeof(data);++i,--p)
{
c = 1<<7;
for(j = 0;j<8;j++)
{
k = *p & c;
k>>=(7-j);
c>>=1;
outs<<(unsigned int)k;
}
outs<<endl;
}
}
class Mem
{
private:
double aaa;
char c;
public:
Mem(){aaa = 0;}
};
void main()
{
float i = -0.5f;
Mem m;
GetBinary(cout,i);
cout<<endl;
GetBinary(cout,m);
}