# Converting HexDecimal To Binary - 'C' programming

**URL:** <https://community.unix.com/t/converting-hexdecimal-to-binary-c-programming/235707>\
**Category:** Programming\
**Created:** [March 19, 2009, 1:49pm UTC](https://community.unix.com/t/converting-hexdecimal-to-binary-c-programming/235707 "2009-03-19T13:49:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudharma](https://community.unix.com/letter_avatar/sudharma/32/6_449c533f586939b6e5f246632a46421f.png) [@sudharma](https://community.unix.com/u/sudharma)\
**Post date:** [March 19, 2009, 1:49pm UTC](https://community.unix.com/t/converting-hexdecimal-to-binary-c-programming/235707/1 "2009-03-19T13:49:58Z")

</div>

guys,

Can somebody help me with a Unix 'C' program, to convert the Hexa decimal value to binary value.

Thank you  
Cheers 🙂  
S

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [March 19, 2009, 2:37pm UTC](https://community.unix.com/t/converting-hexdecimal-to-binary-c-programming/235707/2 "2009-03-19T14:37:17Z")

</div>

Homework? What exactly are the requirements? Just output 1s and 0s?

I'd start with some consistency checks on the input (even number of characters, all between 0 and F), sscanf() 2 characters at a time into an unsigned char, loop over the byte with a bitwise AND, shifting the mask right at every iteration, and output a '1' or '0' as needed. Maybe throw in a blank every 8 bits for better readability.

---

<div class="post-metadata">

**Author:** ![sudharma](https://community.unix.com/letter_avatar/sudharma/32/6_449c533f586939b6e5f246632a46421f.png) [@sudharma](https://community.unix.com/u/sudharma)\
**Post date:** [March 19, 2009, 3:42pm UTC](https://community.unix.com/t/converting-hexdecimal-to-binary-c-programming/235707/3 "2009-03-19T15:42:12Z")

</div>

nope!!! not home work.  
actually i want to convert the 0D (Carriage return ) to binary bits. not to the binary ascii values like ( 00001101) but, to some un readable format. Can you please suggest me how i can get this. I am implemeting some HL7 standard which i don't about this.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [March 20, 2009, 1:07am UTC](https://community.unix.com/t/converting-hexdecimal-to-binary-c-programming/235707/4 "2009-03-20T01:07:35Z")

</div>

You mean, like this?

```nohighlight
int val;
sscanf("0D", "%02x", &val);

```
