整数转换为十六进制 [英] Convert integer to hex

查看:178
本文介绍了整数转换为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何在不使用 sprintf() >解决方案

一个整数的每个4位映射到一个十六进制数字,如果这四个位的值小于10,那么它的ASCII表示是'0'+值,否则它是' '+ value - 10。

您可以通过移动和屏蔽来提取每组四位数字。掩码值为0x0f,并假设一个32位整数,右移开始于24位,并为每个连续数字递减四位。


Can anyone please tell me how to convert an integer to hexadecimal in C without using sprintf()

解决方案

Each 4 bits of an integer maps to exactly one hex digit, if the value of those four bits is less than 10, then its ASCII representation is '0' + value, otherwise it is 'A' + value - 10.

You can extract each group of four digits by shifting and masking. The mask value is 0x0f, and assuming a 32bit integer, the right shift starts at 24 bits and decrements by four for each successive digit.

这篇关于整数转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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