2
#include
#include
#include
#include
using namespace std;
void ConvertLongToHexString(long Input,char* output)
{
char *dest=output;
assert(output!=NULL);
while(Input)
{
if(Input%16>10)
{
*dest++='A'+Input%16-10;
}
else
{
*dest++=Input%16+'0';
}
Input=Input/16;
}
*dest++='x';
*dest++='0';
*dest='\0';
int i=0,j=strlen(output)-1;
while(i
{
char temp;
temp=output[i];
output[i]=output[j];
output[j]=temp;
i++;
j--;
}
}
int main()
{
char *output=(char *)malloc(1);
ConvertLongToHexString(10000000,output);
cout<
return 0;
}
发表于 2015-04-24 21:01:29
回复(0)
1
void ConvertLongToHexString(long Input,char* output) {
static char HexStr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
unsigned char * p = (unsigned char *)(&Input);
for ( int i=0;i
output[i*2] = HexStr[p[i]>>4];
output[i*2+1] = HexStr[(p[i]<<4)>>4];
}
}
发表于 2015-05-13 15:17:54
回复(1)
1
1.我的思路是: 把这个long型(32bit)的数据每次右移4位 ,这4位数据和
1111进行相与,结果放入字符数组的末尾(也可以用栈来实现)
int _tmain(int argc, _TCHAR* argv[])
{
char out[11] = {0};
ConvertLongToHexString(1120, out);
printf("%s",out);
system("pause");
return 0;
}
void ConvertLongToHexString(long Input,char* output)
{
long in = Input;
for(int i = 0; i < 8; i++)
{
int temp = in & 0x0000000F;
output[9-i] = (temp < 10) ? ('0' +temp) : ('A' + temp -10);
in = in >> 4; //右移4位
}
output[10] = '\0';
output[0] = '0';
output[1]