我怎么能转换成整数在C十六进制字符串? [英] How can I convert an integer to a hexadecimal string in C?

查看:110
本文介绍了我怎么能转换成整数在C十六进制字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以转换在C十六进制字符串整数?

How can I convert an integer to a hexadecimal string in C?

实例:整数 50 将被转换为十六进制字符串320x32

Example: The integer 50 would be converted to the hexadecimal string "32" or "0x32".

推荐答案

这code

int a = 5;
printf("%x\n", a);

打印

5


这code


This code

int a = 5; 
printf("0x%x\n", a);

打印

0x5


这code


This code

int a = 89778116;
printf("%x\n", a);

打印

559e7c4


如果你利用的格式是大写的十六进制值的X:


If you capitalize the x in the format it capitalizes the hex value:

int a = 89778116;
printf("%X\n", a);

打印

559E7C4


如果您想打印指针您使用P格式说明:


If you want to print pointers you use the p format specifier:

char* str = "foo";
printf("0x%p\n", str);

打印

0x01275744

这篇关于我怎么能转换成整数在C十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆