当我将变量声明为: unsigned char myargv[] = {0x00,0xFF}; 时,我想将用户从命令行插入的内容打印为 HEX 它工作正常,我得到:11111111 但是当我从命令行传递我的参数时,我得到不同的值示例:myApp.exe FF 我得到:01100010
#include <iostream>
#include <string>
using namespace std;
void writeToScreen(unsigned char *data);
int main(int argc,unsigned char *argv[]){
if(argc != 2){
unsigned char myargv[] = {0x00,0xFF};
writeToScreen(&myargv[1]);
}else{
writeToScreen(argv[1]);
}
system("pause");
return 0;
}
void writeToScreen(unsigned char *data){
unsigned char dat;
dat =*(data);
for (unsigned int i=0;i<8;i++)
{
if (dat & 1)
cout<<"1";
else
cout<<"0";
dat>>=1;
}
cout<<endl;
}