#include <stdio.h>
void bytes2hex(int value, char *out_buf, char bytes)
{
char *p;
inline char bcd2hex(char BCD)
{
return (BCD > 9) ? (BCD-10+'A'):(BCD+'0');
}
inline void char2hex(char value,char **out)
{
*(*out)++ = bcd2hex(value >> 4);
*(*out)++ = bcd2hex(value & 0x0f);
}
p = out_buf;
*p++ = '0';
*p++ = 'x';
if(bytes > 3)char2hex(value >> 24,&p);
if(bytes > 2)char2hex(value >> 16,&p);
if(bytes > 1)char2hex(value >> 8,&p);
char2hex(value &0xff,&p);
*p = 0;
}
int main(int argc,char *argv[])
{
int value;
char out_str[12];
value = 0x12345678;
bytes2hex(value,out_str,sizeof(char));
printf("-sizeof(char)---%s----\n",out_str);
bytes2hex(value,out_str,sizeof(short));
printf("-sizeof(short)---%s----\n",out_str);
bytes2hex(value,out_str,3);
printf("-3---%s----\n",out_str);
bytes2hex(value,out_str,sizeof(long));
printf("-sizeof(long)---%s----\n",out_str);
}
|