- #include <stdio.h>
- #include <stdlib.h>
- char * itoa_hex(unsigned int data,char * hbuff)
- {
- unsigned char * p=(unsigned char *)(&data);
- int len=sizeof(data);
- static const char hex_map[]="0123456789ABCDEF";
- static const bool lit_end=*(char*)(&len)==len;
- int counter=0;
- if(lit_end)
- {
- for(counter=len;counter>0;--counter)
- {
- hbuff[(len-counter)<<1]=hex_map[*(p+counter-1)>>4];
- hbuff[((len-counter)<<1)|1]=hex_map[(*(p+counter-1))&0XF];
- }
- }
- else
- {
- for(;counter<len;++counter)
- {
- hbuff[counter<<1]=hex_map[*(p+counter-1)>>4];
- hbuff[(counter<<1)|1]=hex_map[*(p+counter-1)|0X0F];
- }
- }
- return hbuff;
- }
- int main(int argc, char * argv[])
- {
- char buff[32]="";
- itoa_hex(atoi(argv[1]),buff);
- printf("%s/n",buff);
- return 0;
- }
整数转换成16进制字符串的程序
最新推荐文章于 2024-03-21 15:20:43 发布
本文介绍了一个将无符号整数转换为十六进制字符串的 C 语言函数 itoa_hex。该函数接受一个无符号整数和一个字符缓冲区作为参数,并将整数转换为对应的十六进制形式存储在缓冲区内。文章还提供了一个简单的 main 函数用于演示如何使用此函数。
1447

被折叠的 条评论
为什么被折叠?



