海之石

导航

 
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
int main()
{
        int  num = 204;
        char hexchar[18] = "0123456789abcdef";
        char* a = (char*)malloc(20 * sizeof(char));
        int  idx = 0;
        //转换,位运算
        while(num > 0)
        {
                a[idx++] = hexchar[num & 0xf];
                num = num >> 4;
        }
        //字符串逆序输出
        char* head = a;
        char* tail = a;

        while(*tail)
        {
                tail++;
        }
        tail-- ;
        if(tail == NULL)
        {
                return 0;
        }

        while(tail > head)
        {
                char  tmp= *head;
                *head = *tail;
                *tail = tmp;
                head++;  
                tail--;
        }
        printf("0x%s\n", a);
        free(a);
        return 0;
}        

数字通过位运算转换十六进制

posted on 2017-08-17 20:44  海之石  阅读(898)  评论(0编辑  收藏  举报