64,639
社区成员
发帖
与我相关
我的任务
分享
int main()
{
string hex = "0123456789ABCDEF";
decltype(hex.size()) n = 0;
while (n <= hex.size())
{
hex[n] = 'F';
cout << "hex[" << n << "] address is " << &hex << "\t" << (&hex) + n - 48 << endl;
n++;
}
cout << hex << endl;
system("pause");
return 0;
}
void HexDump(char *buf,int len,int addr) {
int i,j,k;
char binstr[80];
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%08x -",i+addr);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
}
}
乍看起来c++的cin、cout、ifstream、ofstream、istringstream、ostringstream在输入、输出上比c的scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf简单,不用格式控制符!
但是不用格式控制符,输入输出恰好是你期望的格式的时候好说;等到输入输出不是你期望的格式的时候,你就会觉得还是用格式控制符更方便、更靠谱。
摒弃cin、cout、ifstream、ofstream、istringstream、ostringstream!
使用scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf。
#include <iostream>
using namespace std;
inr main(){
string shex ="0123456789ABCDEF";
cout<<"Value =" <<shex<<endl; //输出字符串的值
cout <<"Object adress =" <<&shex <<endl;//输出string 对象shex 的地址
cout <<"The string adress ="<<(void*) (&shex[0])<<endl;//输出串地址
cin.get();
return 0;
}
PS:
hex 是std 名空间内,的输入输出操纵子,是经常会用到的
因此,定义变量最好不要跟他同名
不然输出的时候,会遇到麻烦,至少会给自己带来困扰。
string shex ="0123456789ABCDEF";
cout<<"Value =" <<shex<<endl; //输出字符串的值
cout <<"Object adress =" <<&shex <<endl;//输出string 对象shex 的地址
cout <<"The string adress ="<<(void*) (&shex[0])<<endl;//输出串地址
cout <<"operon hex adress ="<<(void *)&hex<<endl;//输出操纵子的地址
//Value =0123456789ABCDEF
//Object adress =0x9ffdb0
//The string adress =0x1141650
//operon hex adress =0x403420
cout << &hex << endl;