#include <iostream> using namespace std;
static const char *tohex = "0123456789ABCDEF"; void CharToHex(unsigned char *out,const unsigned char *in,int len) { int i = 0; while(i < len) { *out++ = tohex[in[i] >> 4]; *out++ = tohex[in[i] & 0x0F]; ++i; } }
void bitcount(char* x,int size) { for(int i=0;i<size;i++) { for(int j=0;j<sizeof(char)*8;j++) (*(x+i) & (0X80>> j)) ? cout << "1 " : cout << "0 "; if (i < size-1)cout << ", "; } }
int main(int argc, char *argv[]) { int i; unsigned char *out; CharToHex(out,(const unsigned char*)"中文",4); cout << "Hex:" << out << endl; cout << "Binary:"; bitcount((char *)"中文",4); cout << endl; return 0; }
|
程序输出:
Hex:D6D0CEC4
Binary:1 1 0 1 0 1 1 0 , 1 1 0 1 0 0 0 0 , 1 1 0 0 1 1 1 0 , 1 1 0 0 0 1 0 0
阅读(2411) | 评论(0) | 转发(0) |