# Binary to hexadecimal conversion in c

This post presents a nice and efficient algorithm to convert an integer into a hexadecimal string using the properties of binary numbers. Source code in the C language is provided along with binary to hexadecimal conversion in c.

Just quickly sharing an algorithm to **binary to hexadecimal conversion in c** an unsigned decimal integer to a string of hexadecimals.

Normally to do this as a human, the first way that everyone is taught is to progressively divide by the base 16 and keep the remainders. While this is not wrong, there are of course much faster ways and I would like to share one in this post. There is a reason that hexadecimals are universally utilized in computing in order to represent numbers. They are very well fit to do so.

Hexadecimals are numbers with 16 as a base, which is a power of 2. It looks much more presentable now, right? But what have we achieved?

Well by dividing the big integer into groups of 4 bits we have almost already converted it into a hexadecimal. Well a string of 4 bits can represent numbers 0 to For the solution is shown below. Putting all of the above into C code is very easy. By sacrificing 16 bytes of memory to keep an array of the hexadecimal digits we can convert all possible integers.

The function should be simple enough to understand without any explanation. It takes every 4 bits of the integer and matches the corresponding hex digit which it saves inside the buffer. Of course since this produces the reverse of the hexadecimal we want since we read the number from the LSB we need to reverse it in the end. That is achieved by simple XOR swapping. To use the function you would do something like below. In addition if the post caught your interest and you love C programming in general why not check my work in the Refu Library?

It currently needs more contributors and people who are able binary to hexadecimal conversion in c willing to provide feedback! Hey Michael, Thanks for the comment. This is rather an old post and I moved it from another host so the html characters appeared out of the blue. Your email address will not be published. Notify me of follow-up comments by email. Notify me of new posts by email. Skip to content This post presents a nice and efficient algorithm to convert an integer into a hexadecimal string using the properties of binary numbers.

Binary Representation of a Decimal Hexadecimals are numbers with 16 as a base, which is a power of 2. Binary Number divided into groups of 4 bits It looks much more presentable now, right? Hexadecimal conversion Putting all of the above into C code is very easy. Are they not equivalent? Leave a Reply Cancel binary to hexadecimal conversion in c Your email address will not be published.

Counting Bits in C. An introductory tutorial to CFEngine and Ubuntu.

This means a submission must assume that mitigations such as DEP, ASLR, CFG, CIG, ACG, and so on are all enabled by the target application even if they are not currently enabled by default in all scenarios at the time of submission.

This scope is subject to change at any time at Microsofts discretion. Control-flow integrity mitigations The following mitigations support Microsofts control-flow integrity protections.