1.将Long整型转为二进制
#include<iostream>
#include <vector>
#include <assert.h>
#include <string>
using namespace std;
char *get2String(unsigned long num)
{
char *buff = new char[33];
long temp;
for (int i = 0; i < 32; i++)
{
temp = num&(1 << (31 - i));
temp = temp >> (31 - i);
buff[i] = (temp == 0 ? '0' : '1');
}
buff[32] = '\0';
return buff;
}
int main(void)
{
cout << get2String(1024) << endl;
return(0);
}
注意:没有考虑符号为负的情况
2.将Long整型转为十六进制
#include<iostream>
#include <vector>
#include <assert.h>
#include <string>
using namespace std;
char *get16String(unsigned long num)
{
char *buff = new char[11];
buff[0] = '0';
buff[1] = 'x';
buff[10] = '\0