- 论坛徽章:
- 0
|
#include <stdio.h>
#include <string.h>
char hex2bcd(char n)
{
if ( 0 <= n && n < 100)
return ((n/10) << 4 + n%10);
else return 0xff;
}
int voidhex2bcd(int n, char *str)
{
int i;
char x;
char *ptr = str;
char tmp[10] = {0};
int m = n;
for (i = 0; i < sizeof(int); i++)
{
//x =*((char *)&m) & 0xff;
x = m&0x0ff;
tmp = (m&0xff);
//tmp &= 0x00ff;
printf("m%d = %x tmp---[%d]=%x x = %x\n", i, m&0xff,i, tmp,x);
tmp = tmp%100;
printf("1 tmp[%d] = %x\n", i, tmp);
tmp &= 0xff;
tmp[i+1] = tmp/100;
printf("tmp[%d] = %0x\n", i, tmp);
tmp = hex2bcd(tmp);
ptr[2*i] = (tmp>>4) & 0x0f + 0x30;
ptr[2*i + 1] = tmp & 0x0f +0x30;
m >>= 8;
}
printf("tmp %s\n", tmp);
for (i = 0; i < 4; i++)
{
printf("tmp[%d] = %x ", i, tmp);
}
return 0;
}
int main()
{
int n= 1234;
char str[10] = {0};
voidhex2bcd(n, str);
printf("str %s\n", str);
return 0;
}
m0 = d2 tmp---[0]=ffffffd2 x = ffffffd2
1 tmp[0] = ffffffd2
tmp[0] = ffffffd2m1 = 4 tmp---[1]=4 x = 4
1 tmp[1] = 4
tmp[1] = 4
m2 = 0 tmp---[2]=0 x = 0
1 tmp[2] = 0
tmp[2] = 0
m3 = 0 tmp---[3]=0 x = 0
1 tmp[3] = 0
tmp[3] = 0
tmp �
tmp[0] = ffffffff tmp[1] = 0 tmp[2] = 0 tmp[3] = 0 str ??
|
|