天天看点

十进制long数据转化为十六进制,十六进制转化为十进制

{

   int i = 0;

   int tmp = 0;

   char *buffer = (char *)malloc(11);

   char *temp;

   buffer[0] = '0', buffer[1] = 'x', buffer[10] = '\0';

   temp = buffer + 2;

   for (i = 0; i < 8; i++)

   {

     tmp = num & 0x0000000f;

     printf("tmp=%d num=%d, num & 0x0000000f=%d\n", tmp, num, num & 0x0000000f);

     temp[7 - i] = (tmp < 10) ? ('0' + tmp) : ('A' + tmp - 10);

printf("temp[9 - i]=%d\n", temp[9 - i]);

      num = num >> 4;

   }

    printf("buffer=%s\n", buffer );

if(buffer != NULL)

{

free(buffer);

}

}

位操作,实现取10的余数

而下面则是去16余数,然后反序

{

char str[32] = "";

long x1 = 0xabc;

long x2 = 0;

int x3 = 0;

char c1;

x2 = x1 / 0x10;

int i = 0;

printf("x1 = %d x2 = %d\n", x1, x2);

while(x2 > 0)

{

x1 = x1 % 0x10;

printf("< x1 = %d x2 = %d x3 = %d", x1, x2, x3);

if(x1 < 10)

x1 = x1;

else

{

x1 = 'A' + x1 - 10;

}

str[i++] = x1;

x1 = x2;

x2 = x1 / 0x10;

}

if(x1 <= 10)

str[i++] = 'A' + x1 - 10;

printf("11 str=%s\n", str );

for(int j = i - 1; j >= i / 2; j --)

{

c1 = str[j];

str[j] = str[i - j - 1];

str[i - j - 1] = c1;

}

__asm(" MOVN A,#1");

printf("22 str=%s\n", str );

}